HDU 1269 Tarjan

迷宮城堡

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10558    Accepted Submission(s): 4737


Problem Description
爲了訓練小希的方向感,Gardon建立了一座大城堡,裏面有N個房間(N<=10000)和M條通道(M<=100000),每個通道都是單向的,就是說若稱某通道連通了A房間和B房間,只說明可以通過這個通道由A房間到達B房間,但並不說明通過它可以由B房間到達A房間。Gardon需要請你寫個程序確認一下是否任意兩個房間都是相互連通的,即:對於任意的i和j,至少存在一條路徑可以從房間i到房間j,也存在一條路徑可以從房間j到房間i。
 

Input
輸入包含多組數據,輸入的第一行有兩個數:N和M,接下來的M行每行有兩個數a和b,表示了一條通道可以從A房間來到B房間。文件最後以兩個0結束。
 

Output
對於輸入的每組數據,如果任意兩個房間都是相互連接的,輸出"Yes",否則輸出"No"。
 

Sample Input
3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0
 

Sample Output
Yes No
 

Author
Gardon
 

Source
 

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long  
using namespace std;



/************************************************

Desiner:hl
time:2015/11/06
Exe.Time:78MS
Exe.Memory:4356K

題意:親切的中文題。。
題解:tarjan 。。裸模板題 
修改了一下kuangbin神的版 orz 這樣看起來方便處理 
************************************************/


const int MAXN = 50010;
const int MAXM = 100010;
struct Edge
{
    int to,next,st;
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
int Index,top;
int scc;
bool Instack[MAXN];
int inde[MAXN],oude[MAXN];
void addedge(int u,int v)
{
    edge[tot].st=u;edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}
void Tarjan(int u)  //kuangbin的tarjan模板 
{
    int v;
    DFN[u] = Low[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if(!DFN[v])
        {
            Tarjan(v);
            if( Low[u] > Low[v] )Low[u] = Low[v];
        }
        else if(Instack[v] && Low[u] > DFN[v])Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
        }
        while( v != u );
    }
}
void solve(int n)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    Index = scc = top = 0;
    for(int i = 1;i <= n;i++)
        if(!DFN[i])
            Tarjan(i);
}
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

int main(){
	int i,j,n,m,a,b;
	int cost[1111];
	while(~scanf("%d%d",&n,&m)&&n+m){
		init();
//		for(i=1;i<=n;i++){
//			scanf("%d",&cost[i]);
//		}
		for(i=1;i<=m;i++){
			scanf("%d %d",&a,&b);
			addedge(a,b);
		}
		solve(n);
		if(scc==1||n==1) printf("Yes\n");
		else printf("No\n");
//		memset(inde,0,sizeof(inde));
//		for(i=0;i<tot;i++){
//			a=edge[i].st;
//			b=edge[i].to;
//			if(Belong[a]!=Belong[b]){
//				inde[Belong[b]]++;
//				oude[Belong[a]]++;
//			}
//		}
//
//		int ans1,ans2;
//		ans1=ans2=0;
//		int minans[MAXN];
//		for(i=1;i<=scc;i++){
//			if(inde[i]==0)
//				ans1++;
//				
//			minans[i]=inf;
//		}
//		for(i=1;i<=n;i++){
//			int tmp=Belong[i];
//			if(inde[tmp]==0){
//				minans[tmp]=min(minans[tmp],cost[i]);
//			}
//		}
//		for(i=1;i<=scc;i++){
//			if(minans[i]!=inf){
//				ans2+=minans[i];
//			}
//		}
//
//		printf("%d %d\n",ans1,ans2);
		
		
		
		
	}
	
	
}

發佈了80 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章