• [题解] poj 2762 Going from u to v or from v to u? (tarjan强连通分量)


    - 传送门 -

     http://poj.org/problem?id=2762

    #Going from u to v or from v to u?

    | Time Limit: 2000MS |   | Memory Limit: 65536K |
    | Total Submissions: 18189 |   | Accepted: 4868 |

    Description

    In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn't know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

    Input

    The first line contains a single integer T, the number of test cases. And followed T cases.

    The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

    Output

    The output should contain T lines. Write 'Yes' if the cave has the property stated above, or 'No' otherwise.

    Sample Input

    1
    3 3
    1 2
    2 3
    3 1

    Sample Output

    Yes

    Source

    POJ Monthly--2006.02.26,zgl & twb

    - 题意 -

     给一些有向边, 判断图中两点是否联通(从 x 走到 y, 或从 y 走到 x, 满足其中至少一个)
     

    - 思路 -

     tarjan缩点后, 我们发现, 一定是一条链才满足条件(两条分岔路上的点无法联通).
     暴力判链...慢如狗...
     注意整张图为一个强连通分量的情况.
     
     细节见代码.
     

    - 代码 -

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    const int N = 1e3 + 5;
    const int M = 6e3 + 5;
    
    int IN[N], OUT[N];
    int STK[N];
    int DFN[N], BL[N], LOW[N];
    int NXT[M], TO[M], HD[N];
    int cas, n, m, sz, cnt, tot, top;
    vector<int> G[N];
    
    void init() {
    	memset(HD, 0 ,sizeof (HD));
    	memset(DFN, 0 ,sizeof (DFN));
    	memset(BL, 0, sizeof (BL));
    	memset(IN, 0, sizeof (IN));
    	memset(OUT, 0, sizeof (OUT));
    	sz = cnt = tot = top = 0;
    }
    
    void tarjan(int x) {
    	DFN[x] = LOW[x] = ++tot;
    	STK[++top] = x;
    	for (int i = HD[x]; i; i = NXT[i]) {
    		int v = TO[i];
    		if (!DFN[v]) {
    			tarjan(v);
    			LOW[x] = min(LOW[x], LOW[v]);
    		}
    		else if (!BL[v]) LOW[x] = min(LOW[x], DFN[v]);
    	}
    	if (LOW[x] == DFN[x]) {
    		cnt ++;
    		int tmp;
    		G[cnt].clear();
    		do {
    			tmp = STK[top--];
    			BL[tmp] = cnt;
    			G[cnt].push_back(tmp);
    		}while (tmp != x);
    	}
    }
    
    int main() {
    	scanf("%d", &cas);
    	while (cas--) {
    		init();
    		scanf("%d%d", &n, &m);
    		for (int i = 1; i <= m; ++i) {
    			int a, b;
    			scanf("%d%d", &a, &b);
    			TO[++sz] = b;
    			NXT[sz] = HD[a];
    			HD[a] = sz;
    		}
    		for (int i = 1; i <= n; ++i)
    			if (!DFN[i]) tarjan(i);
    		for (int i = 1; i <= cnt; ++i) {
    			for (int j = 0; j < G[i].size(); ++j) {
    				int u = G[i][j];
    				for (int k = HD[u]; k; k = NXT[k]) {
    					int v = TO[k];
    					if (i != BL[v]) {
    						IN[BL[v]] ++;
    						OUT[i] ++;
    					}
    				}
    			}
    		}
    		int f1 = -1, f2 = -1;
    		for (int i = 1; i <= cnt; ++i) {
    			if (IN[i] == 0) f1 ++;
    			else if (OUT[i] == 0) f2 ++;
    			else if (IN[i] != 1 || OUT[i] != 1) {
    				f1 = f2 = -2;
    				break;
    			}
    		}
    		if ((f1 == 0 && f2 == 0) || cnt == 1) printf("Yes
    "); //注意判 cnt==1 的情况
    		else printf("No
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    HDU2732 Leapin' Lizards 网络流 最大流 SAP
    POJ1459 Power Network 网络流 最大流
    HDU3718 Similarity KM
    HDU3488 Tour KM
    HDU2853 Assignment KM
    HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色
    POJ1469 COURSES 二分图匹配 匈牙利算法
    HDU4185 Oil Skimming 二分图匹配 匈牙利算法
    POJ3041 Asteroids 二分图匹配 匈牙利算法
    BZOJ2553 [BeiJing2011]禁忌 AC自动机 矩阵
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7403468.html
Copyright © 2020-2023  润新知