Exploration

 Accepts: 190
 Submissions: 976
 Time Limit: 30000/15000 MS (Java/Others)
 Memory Limit: 131072/131072 K (Java/Others)
Problem Description

Miceren likes exploration and he found a huge labyrinth underground!

This labyrinth has NN 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.

Input

The first line contains a single integer TT, indicating the number of test cases.

Each test case begins with three integers N,~M1,~M2N, M1, M2, indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.

The next M1M1 lines contain the details of the undirectional tunnels. Each line contains two integers u,~vu, v meaning that there is a undirectional tunnel between u,~vu, v. (u~\neq~vu ≠ v)

The next M2M2 lines contain the details of the directional tunnels. Each line contains integers u,~vu, v meaning that there is a directional tunnel from uu to vv. (u~\neq~vu ≠ v)

TT is about 100.

1~\le~N, M1, M2~\le~1000000.1 ≤ N,M1,M2 ≤ 1000000.

There may be some tunnels connect the same pair of caves.

The ratio of test cases with $N~\gt~1000$ is less than 5%.

Output

For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".

Sample Input
2
5 2 1
1 2
1 2
4 5
4 2 2
1 2
2 3
4 3
4 1
Sample Output
YES
NO
Hint

If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.

思路:并查集+拓扑排序;
先处理无向边,用并查集缩点,然后处理完后,所有的无向边都没了,并且无向边所连的点都缩成一个点,那么剩下的就是有向图了,那么有向图判环用拓扑排序。
  1 #include <stdio.h>
2 #include <stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<math.h>
6 #include<string.h>
7 #include<map>
8 #include<vector>
9 #include<queue>
10 using namespace std;
11 #pragma comment(linker, "/STACK:102400000,102400000")
12 typedef long long LL;
13 int bin[1000005];
14 int du[1000005];
15 vector<int>vec[1000005];
16 queue<int>que;
17 int id[1000005];
18 bool dfs(int n);
19 int fin(int x);
20 int cnt[1000005];
21 bool t[1000005];
22 int main(void)
23 {
24 int n;
25 scanf("%d",&n);
26 while(n--)
27 {
28 int i,j;
29 memset(cnt,0,sizeof(cnt));
30 memset(t,0,sizeof(t));
31 for(i = 0; i <= 1000000; i++)
32 {
33 bin[i] = i;
34 du[i] = 1;
35 vec[i].clear();
36 }
37 int N,m1,m2;
38 bool flag = false;
39 scanf("%d %d %d",&N,&m1,&m2);
40 while(m1--)
41 {
42 int a,b;
43 scanf("%d %d",&a,&b);
44 int aa = fin(a);
45 int bb = fin(b);
46 if(aa!=bb)
47 {
48 if(du[aa]>du[bb])
49 {
50 du[aa]+=du[bb];
51 bin[bb] = aa;
52 }
53 else
54 {
55 du[bb] += du[aa];
56 bin[aa] = bb;
57 }
58 }
59 else flag = true;
60 }
61 while(m2--)
62 {
63 int a,b;
64 scanf("%d %d",&a,&b);
65 if(!flag)
66 {
67 int aa = fin(a);
68 int bb = fin(b);
69 if(aa == bb)
70 flag = true;
71 else
72 { cnt[bb]++;
73 vec[aa].push_back(bb);
74 }
75 }
76 }
77 if(!flag)
78 {
79 int cn = 0;
80 for(i = 1; i <= N; i++)
81 {
82 int c = fin(i);
83 if(!t[c])
84 {
85 id[cn++] =c;
86 t[c] = true;
87 }
88 }
89 while(!que.empty())que.pop();
90 int ck = 0;
91 for(i = 0;i < cn;i++)
92 {
93 if(!cnt[id[i]])
94 {
95 que.push(id[i]);
96 ck++;
97 }
98 }
99 while(!que.empty())
100 {
101 int ff = que.front();
102 que.pop();
103 for(i = 0;i < vec[ff].size();i++)
104 {
105 int k = vec[ff][i];
106 cnt[k]--;
107 if(cnt[k] == 0)
108 ck++,que.push(k);
109 }
110 }
111 if(cn != ck)
112 flag = true;
113 }
114 if(flag)printf("YES\n");
115 else printf("NO\n");
116 }
117 return 0;
118 }
119 int fin(int x)
120 {
121 int i;
122 for(i = x; bin[i] != i;)
123 i = bin[i];
124 return i;
125 }

还有拓扑排序部分改成dfs也可以过,数据比较水。

  1 #include <stdio.h>
2 #include <stdlib.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<math.h>
6 #include<string.h>
7 #include<map>
8 #include<vector>
9 using namespace std;
10 #pragma comment(linker, "/STACK:102400000,102400000")
11 typedef long long LL;
12 int bin[1000005];
13 int du[1000005];
14 vector<int>vec[1000005];
15 bool vis[1000005];
16 int id[1000005];
17 bool t[1000005];
18 bool dfs(int n);
19 int fin(int x);
20 int cnt[1000005];
21 int main(void)
22 {
23 int n;
24 scanf("%d",&n);
25 while(n--)
26 {
27 int i,j;
28 memset(vis,0,sizeof(vis));
29 memset(t,0,sizeof(t));
30 memset(cnt,0,sizeof(cnt));
31 for(i = 0; i <= 1000000; i++)
32 {
33 bin[i] = i;
34 du[i] = 1;
35 vec[i].clear();
36 }
37 int N,m1,m2;
38 bool flag = false;
39 scanf("%d %d %d",&N,&m1,&m2);
40 while(m1--)
41 {
42 int a,b;
43 scanf("%d %d",&a,&b);
44 int aa = fin(a);
45 int bb = fin(b);
46 if(aa!=bb)
47 {
48 if(du[aa]>du[bb])
49 {
50 du[aa]+=du[bb];
51 bin[bb] = aa;
52 }
53 else
54 {
55 du[bb] += du[aa];
56 bin[aa] = bb;
57 }
58 }
59 else flag = true;
60 }
61 while(m2--)
62 {
63 int a,b;
64 scanf("%d %d",&a,&b);
65 if(!flag)
66 {
67 int aa = fin(a);
68 int bb = fin(b);
69 if(aa == bb)
70 flag = true;
71 else
72 {
73 vec[aa].push_back(bb);
74 }
75 }
76 }
77 if(!flag)
78 {
79 int cn = 0;
80 for(i = 1; i <= N; i++)
81 {
82 int c = fin(i);
83 if(!t[c])
84 {
85 id[cn++] =c;
86 }
87 }
88 for(i = 0; i <= cn; i++)
89 {
90 if(!vis[id[i]])
91 {
92 flag = dfs(id[i]);
93 if(flag)break;
94 }
95 }
96 }
97 if(flag)printf("YES\n");
98 else printf("NO\n");
99 }
100 return 0;
101 }
102 int fin(int x)
103 {
104 int i;
105 for(i = x; bin[i] != i;)
106 i = bin[i];
107 return i;
108 }
109 bool dfs(int n)
110 {
111 cnt[n] = 1;
112 vis[n] = true;
113 for(int i = 0; i < vec[n].size(); i++)
114 {
115 int c = vec[n][i];
116 if(cnt[c] == 1)
117 {
118 cnt[c] = 0;
119 return true;
120 }
121 else
122 {
123 if(dfs(c))
124 {
125 cnt[c] = 0;
126 return true;
127 }
128 }
129 }
130 cnt[n] = 0;
131 return false;
132 }

Exploration(hdu5222)的更多相关文章

  1. HDU-5222 Exploration(拓扑排序)

    一.题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5222 二.题意 给一个无向边+有向边的混合图,其中每条边只能使用一次,问图中是否存在环. 三.思路 ...

  2. poj 2594 Treasure Exploration (二分匹配)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2 ...

  3. RFID Exploration and Spoofer a bipolar transistor, a pair of FETs, and a rectifying full-bridge followed by a loading FET

    RFID Exploration Louis Yi, Mary Ruthven, Kevin O'Toole, & Jay Patterson What did you do? We made ...

  4. POJ2594 Treasure Exploration

    Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8193   Accepted: 3358 Description Have ...

  5. 并查集+拓扑排序 赛码 1009 Exploration

    题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...

  6. poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)

    http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total ...

  7. [UVA] 784 - Maze Exploration

      Maze Exploration  A maze of rectangular rooms is represented on a two dimensional grid as illustra ...

  8. Treasure Exploration(二分最大匹配+floyd)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7455   Accepted: 3 ...

  9. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

随机推荐

  1. 6. Reverse Linked List 逆转单链表

    逆转单链表,比较简单,不细讲,扫描依次改变指针指向. class Solution { public: ListNode* reverseList(ListNode* head) { if(head= ...

  2. Hi3516开发笔记(六):通过HiTools使用USB/串口将uboot、kernel、roofts和userdata按照分区表烧写镜像

    若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/121706033红胖子(红模仿)的博文大全:开发技术集合( ...

  3. idea数据库报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    通过idea操作数据库,进行数据的增加,运行时报错java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 原因:没有导入mysql-connec ...

  4. 如何通过 User-Agent 识别百度蜘蛛

    如果有大量的百度蜘蛛抓取网站就需要注意了:有可能是其他爬虫伪造百度蜘蛛恶意抓取网站. 如果遇到这种情况,这时候就需要查看日志来确定是不是真正的百度蜘蛛(baidu spider).搜索引擎蜘蛛.用户访 ...

  5. Oracle中的DBMS_LOCK包的使用

    一.DBMS_LOCK相关知识介绍 锁模式: 名字 描述 数据类型 值 nl_mode Null INTEGER 1 ss_mode Sub Shared: used on an aggregate ...

  6. 数组实现堆栈——Java实现

    1 package struct; 2 3 4 //接口 5 interface IArrayStack{ 6 //栈的容量 7 int length(); 8 //栈中元素个数(栈大小) 9 int ...

  7. 【Linux】【CentOS】【FTP】FTP服务器安装与配置(vsftpd、lftp)

    [初次学习.配置的笔记,如有不当,欢迎在评论区纠正 -- 萌狼蓝天 @ 2021-12-02] 基本概念 FTP访问方式 实体账号:本地账户 来宾账户:guest 匿名登录:anonymous fp ...

  8. python实现skywalking的trace模块过滤和报警

    skywalking本身的报警功能,用起来视乎不是特别好用,目前想实现对skywalking的trace中的错误接口进行过滤并报警通知管理员和开发.所以自己就用python对skywalking做了二 ...

  9. 带你了解 Angular 与 Angular JS

    Angular 是一个基于 TypeScript 的开源客户端框架,专为构建 Web 应用程序而设计. 另一方面,AngularJS 是 Angular 的第一个版本,用纯 JavaScript 编写 ...

  10. EmmyLua 注解功能

    前言 网上配置 EmmyLua 的方法很多,此处就不做赘述(因此前提是你已经安装配置完EmmyLua) 本文仅是对 EmmyLua插件 内 注解功能 用法的代码演示.因为网上大部分EmmyLua配置教 ...