[POJ2762]Going from u to v or from v to u?

题目大意:
判断一个有向图是否弱连通。

思路:
Tarjan缩点。然后判断原图是否是一条链。
考虑链的特性:有且仅有一点入度为0,有且仅有一点出度为0。
因此缩点后直接判断入度为0和出度为0的点的个数是否均为1即可。

 1 #include<stack>
 2 #include<cstdio>
 3 #include<cctype>
 4 #include<vector>
 5 #include<cstring>
 6 inline int getint() {
 7     char ch;
 8     while(!isdigit(ch=getchar()));
 9     int x=ch^‘0‘;
10     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^‘0‘);
11     return x;
12 }
13 const int V=1001;
14 std::vector<int> e[V];
15 inline void add_edge(const int u,const int v) {
16     e[u].push_back(v);
17 }
18 int dfn[V],low[V],scc[V],in[V],out[V],cnt,id;
19 bool ins[V];
20 std::stack<int> s;
21 inline void init() {
22     for(int i=0;i<V;i++) e[i].clear();
23     memset(dfn,0,sizeof dfn);
24     memset(low,0,sizeof low);
25     memset(scc,0,sizeof scc);
26     memset(ins,0,sizeof ins);
27     memset(in,0,sizeof in);
28     memset(out,0,sizeof out);
29     cnt=id=0;
30 }
31 void Tarjan(const int x) {
32     dfn[x]=low[x]=++cnt;
33     s.push(x);
34     ins[x]=true;
35     for(unsigned i=0;i<e[x].size();i++) {
36         int &y=e[x][i];
37         if(!dfn[y]) {
38             Tarjan(y);
39             low[x]=std::min(low[x],low[y]);
40         }
41         else if(ins[y]) {
42             low[x]=std::min(low[x],dfn[y]);
43         }
44     }
45     if(dfn[x]==low[x]) {
46         int y;
47         ++id;
48         do {
49             y=s.top();
50             s.pop();
51             ins[y]=false;
52             scc[y]=id;
53         } while(y!=x);
54     }
55 }
56 int main() {
57     for(int T=getint();T;T--) {
58         int n=getint();
59         init();
60         for(int m=getint();m;m--) {
61             int u=getint(),v=getint();
62             add_edge(u,v);
63         }
64         for(int i=1;i<=n;i++) {
65             if(!dfn[i]) Tarjan(i);
66         }
67         for(int x=1;x<=n;x++) {
68             for(unsigned i=0;i<e[x].size();i++) {
69                 int &y=e[x][i];
70                 if(scc[x]!=scc[y]) out[scc[x]]++,in[scc[y]]++;
71             }
72         }
73         int cin=0,cout=0;
74         for(int i=1;i<=id;i++) {
75             if(!in[i]) cin++;
76             if(!out[i]) cout++;
77         }
78         puts(cin==1&&cout==1?"Yes":"No");
79     }
80     return 0;
81 }
时间: 2024-12-15 01:53:33

[POJ2762]Going from u to v or from v to u?的相关文章

POJ2762 Going from u to v or from v to u? 强连通+缩点

题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足   随意两点ab 都能 从a到达b 或  从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互相到达的顶点集)  进行缩点 然后再依据原有边 又一次建图 假设缩点后的图是一条单链(回路,通路都能够)   则一定满足弱连通 推断是否是一条单链 能够依据建图过程中得到 入度 出度 数组进行推断 某点的入度 或 出度假设大于1则一定不是单链 另外单链仅仅能有一条  不能有多个点入度=0 代码: #

poj2762 Going from u to v or from v to u? --- 缩点+拓扑

给一个有向图,问是否该图上任意两点间可达. 首先容易想到缩点成有向无环图,其次就是如何处理任意两点间可达. 我在纸上画了一些情况: 4 3 1 2 2 3 2 4 4 4 1 2 1 3 2 4 3 4 3 3 1 2 2 3 1 3 7 8 1 2 1 3 3 4 2 4 4 5 4 6 5 7 6 7 5 6 1 2 1 3 2 3 3 4 3 5 4 5 NNYNY 根据这里一直在纠结如何通过入度出度直接判断.但是一直觉得不严谨.. 然后发现只要拓扑序列唯一即可.这样图中就没有 没有关系的

[poj2762] Going from u to v or from v to u?(Kosaraju缩点+拓排)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14778   Accepted: 3911 Description In order to make their sons brave, Jiajia and Wind take them

【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?

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 o

POJ2762 Going from u to v or from v to u?(强连通缩点+拓扑排序)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15196   Accepted: 4013 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

poj 2762 Going from u to v or from v to u? trajan+拓扑

Going from u to v or from v to u? 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 thei

POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可能从一点到还有一点的. 代码例如以下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include

POJ 2762 Going from u to v or from v to u? (有向图求单连通性)

POJ 2762 Going from u to v or from v to u? 链接:http://poj.org/problem?id=2762 题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中.洞穴中有n 个房间,有一些单向的通道连接某些房间.每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x.Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成

POJ 2762 Going from u to v or from v to u? (判断弱连通)

http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是弱连通的. 首先来一遍强连通缩点,重新建立新图,接下来我们在新图中找入度为0的点,入度为0的点只能有1个,如果有多个那么这些个点肯定是不能相互到达的. 如果只有一个入度为0的点,走一遍dfs判断新图是否是单链,如果有分支,那么分支上的点肯定是不能相互到达的. 1 #include<iostream> 2 #include<algorit

[ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

题目链接: http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14546   Accepted: 3837 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cav