HDU 5222 Exploration
来源:互联网 发布:奇迹归来神器进阶数据 编辑:程序博客网 时间:2024/06/05 11:33
首先对于所有的无向边,我们使用并查集将两边的点并起来
若一条边未合并之前,两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为这两个点处于同一个并查集集合中,那
么它们之间至少存在一条路径)
如果上一步没有判断出环,那么仅靠无向边是找不到环的
考虑到,处于同一个并查集集合中的点之间必定存在一条路径互达,因此将一个集合的点合并之后,原问题等价于在新生成的有向
图中是否有环(包括自环)。。
求一下强连通分量就好了。。。
Exploration
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 333 Accepted Submission(s): 111Problem DescriptionMiceren likes exploration and he found a huge labyrinth underground! This labyrinth hasN caves and some tunnels connecting some pairs of caves. There are two types of tunnel, one type of them can be passed in only one direction and the other can be passed in two directions. Tunnels will collapse immediately after Miceren passing them. Now, Miceren wants to choose a cave as his start point and visit at least one other cave, finally get back to start point. As his friend, you must help him to determine whether a start point satisfing his request exists.InputThe first line contains a single integerT , indicating the number of test cases.Each test case begins with three integersN, M1, M2 , indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels. The nextM1 lines contain the details of the undirectional tunnels. Each line contains two integersu, v meaning that there is a undirectional tunnel betweenu, v . (u ≠ v ) The nextM2 lines contain the details of the directional tunnels. Each line contains integersu, v meaning that there is a directional tunnel fromu tov . (u ≠ v )T is about 100.1 ≤ N,M1,M2 ≤ 1000000. There may be some tunnels connect the same pair of caves.The ratio of test cases withN > 1000 is less than 5%.OutputFor each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".Sample Input25 2 11 21 24 54 2 21 22 34 34 1Sample OutputYESNOHintIf you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.Source赛码"BestCoder"杯中国大学生程序设计冠军赛#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define prt(k) cerr<<#k" = "<<k<<endl#define pb push_back#define MP make_pair#define fi first#define se second#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define SZ(X) ((int)(v).size())#define For(i,l,r) for (int i = l; i <= r; ++i)#define Cor(i,l,r) for (int i = l; i >= r; --i)#define Fill(a,b) memset(a,b,sizeof(a))#define READ freopen("a.in","r",stdin);freopen("a.out","w",stdout)void read(string t){ freopen((t+".in").c_str(),"r",stdin); freopen((t+".out").c_str(),"w",stdout);}const int inf = 0x3f3f3f3f;typedef long long ll;typedef vector<int> VI;typedef vector<VI> VII;typedef pair<int,int> pii;template <class T>void Max(T &a, T b) { a=max(a, b); }#pragma comment(linker, "/STACK:102400000,102400000")const int N = 1001000;struct Edge{int v, next;Edge() {}Edge(int a, int b){v = a, next = b;}} e[N<<2];int head[N], mm;int n, m, m1, m2;void init_edge(){mm = 0;memset(head, -1, sizeof head);}void add(int u, int v){e[mm] = Edge(v, head[u]);head[u] = mm++;}bool flag = false;#include <stack>stack<int> S;int cur, scc;int dfn[N*3], low[N*2], in[N];bool ins[N];void dfs(int u){dfn[u] = low[u] = ++ cur;S.push(u);ins[u] = 1;for (int i = head[u]; ~i; i = e[i].next){int v = e[i].v;if (dfn[v]==0) {dfs(v);low[u] = min(low[u], low[v]);}else {if (ins[v])low[u] = min(low[u], dfn[v]);}}if (low[u] == dfn[u] ) {++ scc; int v;do {v = S.top(); S.pop();ins[v] = 0;in[v] = scc;} while (v - u);}}int fa[N];int f(int x) { return x==fa[x] ? x : fa[x] = f(fa[x]); }int C[N];int main(){int re,ca=1; cin>>re;while (re--){init_edge();cin >> n >> m1 >> m2;for (int i=1;i<=n;i++) fa[i] = i;flag = false;for (int i=0;i<m1;i++) {int u, v;scanf("%d%d", &u, &v);int x = f(u), y = f(v);if (x == y) {flag = true;}else fa[x] = y;}for (int i=0;i<m2;i++) {int u, v;scanf("%d%d", &u, &v);u = f(u); v = f(v);add(u, v);if (u==v) flag = true;}int tot = 0;memset(C,0,sizeof C);for (int i=1;i<=n;i++) {int t = f(i);if (C[t] == 0){C[t] = 1;tot++;}}memset(dfn, 0 , sizeof dfn);memset(low,0,sizeof low);memset(ins, 0, sizeof ins);memset(in, 0, sizeof in);cur = scc = 0;while (!S.empty()) S.pop();for (int i=1; !flag &&i<=n;i++) {int t = f(i);if (dfn[t]==0) dfs(t);}if(scc < tot) flag = true;if (flag) puts("YES");else puts("NO");}return 0;}
2 0
- hdu 5222 Exploration (dfs)
- HDU 5222 Exploration
- hdu 5222 Exploration
- hdu 5222 Exploration
- hdu 5222 Exploration
- HDU 5222 - Exploration (强连通 + 并查集)
- 【HDU】5222 Exploration(并查集+拓扑排序)
- hdu 5222 Exploration(并查集+拓扑排序)
- 【并查集+强连通】 HDU 5222 Exploration
- HDU 5222 Exploration 并查集+强连通分量
- HDU 5222 Exploration 混合图判断是否成环
- HDOJ 5222 Exploration
- hdu Exploration (并查集 拓扑 强连通)
- HDOJ 5222 Exploration 并查集+拓扑排序 找环
- pku3618 Exploration
- Exploration poj
- Data Exploration
- Treasure Exploration
- 设置Tomcat编码
- 4.UI篇 UIKit,view与layer,frame 与bounds等 CoreText auto layout drawRect
- VMware下OSSIM 5.0安装和使用小结
- packstack + vxlan 利用vbox虚拟机搭建1控制节点+2计算节点openstack
- c# 无法加载DLL“###.dll”,: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E),解决办法总结
- HDU 5222 Exploration
- JavaServer Faces (JSF)
- 数据库引擎简介
- 小升初英语如何复习
- java中jni调用不需要配置dll的处理代码
- 绑定的方式调用服务的方法和采用接口方式抽取方法
- 关于xib创建scrollView无法滚动的问题
- Class Project: AVL Tree
- AOP的两种实现方式