【POJ - 2762】Going from u to v or from v to u?(Tarjan缩点,树形dp 或 拓扑排序,欧拉图相关)

题干:

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

题目大意:

问一个图是否是单连通的。

解题报告:

先对全图求一次SCC,可以知道每个SCC内的点都是单连通的,那么把每个SCC缩点构建出DAG之后再判断这个DAG是否单连通即可。方法不唯一:可以是拓扑排序,看他每次是否只有一个活跃点就可以了,如果不是一个,那肯定不符合要求。

第二种方法:是DAG动规找出最长链,如果最长链上的点个数等于SCC个数,那么DAG单连通。(因为如果最长链都不能覆盖所有点,那么必定有两个点之间是无法到达的)

 

刚开始以为直接判断是否是一条链就行了,后来3 -> 1、 1 -> 2、3 -> 2这组数据就Hack掉了,,本应该是Yes,但是这样肯定就是No了。然后想判一个欧拉通路,虽然可以AC,但是有数据是过不了的。(数据水了)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1000 + 5;
struct Edge {int fr,to,ne;
} e[6005],E[6005];
vector<int> vv[MAX];
int head[MAX],HEAD[MAX],tot,TOT;
bool ok[MAX][MAX];
void add(int u,int v) {e[++tot].to = v;e[tot].fr = u;e[tot].ne = head[u];head[u] = tot;
}
int n,m;
int DFN[MAX],LOW[MAX],scc,clk,index,sk[MAX],vis[MAX],col[MAX],in[MAX],out[MAX]; 
void Tarjan(int x) {DFN[x] = LOW[x] = ++clk;vis[x]=1;sk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].to;if(DFN[v] == 0) {Tarjan(v);LOW[x] = min(LOW[x],LOW[v]);}else if(vis[v]) {LOW[x] = min(LOW[x],DFN[v]);}		}if(LOW[x] == DFN[x]) {scc++;while(1) {int tmp = sk[index];index--;vis[tmp]=0;col[tmp] = scc;if(tmp == x) break;}				}	
}
void init() {scc=clk=tot=TOT=index=0;for(int i = 1; i<=n; i++) {vv[i].clear();head[i] = HEAD[i] = -1;vis[i] = DFN[i] = LOW[i] = in[i] = out[i] = 0;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) ok[i][j]=0;}
}
bool topu() {queue<int> q;for(int i = 1; i<=scc; i++) {if(in[i] == 0)  q.push(i);}int cnt = 0;while(!q.empty()) {if(q.size() > 1) return 0 ;int cur = q.front();q.pop();cnt++;int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];in[v]--;if(in[v] == 0) q.push(v);}		}return cnt == scc;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);		init();for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);add(a,b);}for(int i = 1; i<=n; i++) {if(DFN[i] == 0) Tarjan(i);}for(int a,b,i = 1; i<=tot; i++) {a=col[e[i].fr],b=col[e[i].to];if(a==b) continue;if(ok[a][b]) continue;ok[a][b]=1;in[b]++;out[a]++;vv[a].pb(b);}bool flag = topu();if(flag) puts("Yes");else puts("No");}return 0 ;
}

AC代码:(树形dp)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1000 + 5;
struct Edge {int fr,to,ne;
} e[6005],E[6005];
vector<int> vv[MAX];
int head[MAX],HEAD[MAX],tot,TOT;
bool ok[MAX][MAX];
void add(int u,int v) {e[++tot].to = v;e[tot].fr = u;e[tot].ne = head[u];head[u] = tot;
}
int n,m;
int dp[MAX];
int DFN[MAX],LOW[MAX],scc,clk,index,sk[MAX],vis[MAX],col[MAX],in[MAX],out[MAX]; 
void Tarjan(int x) {DFN[x] = LOW[x] = ++clk;vis[x]=1;sk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].to;if(DFN[v] == 0) {Tarjan(v);LOW[x] = min(LOW[x],LOW[v]);}else if(vis[v]) {LOW[x] = min(LOW[x],DFN[v]);}		}if(LOW[x] == DFN[x]) {scc++;while(1) {int tmp = sk[index];index--;vis[tmp]=0;col[tmp] = scc;if(tmp == x) break;}				}	
}
void init() {scc=clk=tot=TOT=index=0;for(int i = 1; i<=n; i++) {vv[i].clear();head[i] = HEAD[i] = -1;vis[i] = DFN[i] = LOW[i] = in[i] = out[i] = dp[i] = 0;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) ok[i][j]=0;}
}
int dfs(int cur){if(dp[cur])return dp[cur];int num=0;int up = vv[cur].size();for(int i = 0; i<up; i++) {int v = vv[cur][i];num=max(num,dfs(v));}return dp[cur]=num+1;
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);		init();for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);add(a,b);}for(int i = 1; i<=n; i++) {if(DFN[i] == 0) Tarjan(i);}for(int a,b,i = 1; i<=tot; i++) {a=col[e[i].fr],b=col[e[i].to];if(a==b) continue;if(ok[a][b]) continue;ok[a][b]=1;in[b]++;out[a]++;vv[a].pb(b);}int flag = 0;for(int i = 1; i<=n; i++){if(dfs(LOW[i]) == scc){flag=true;break;}//或者dfs(i)也可以ac}if(flag) puts("Yes");else puts("No");}return 0 ;
}

这里送上一种虽然错误但是可以AC的代码,引以为戒。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1000 + 5;
struct Edge {int fr,to,ne;
} e[6005],E[6005];
int f[MAX];
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); 
}
void merge(int u,int v) {int t1 = getf(u),t2 = getf(v);f[t2] = t1;
}
int head[MAX],HEAD[MAX],tot,TOT;
bool ok[MAX][MAX];
void add(int u,int v) {e[++tot].to = v;e[tot].fr = u;e[tot].ne = head[u];head[u] = tot;
}
int n,m;
int DFN[MAX],LOW[MAX],scc,clk,index,sk[MAX],vis[MAX],col[MAX],in[MAX],out[MAX]; 
void Tarjan(int x) {DFN[x] = LOW[x] = ++clk;vis[x]=1;sk[++index] = x;for(int i = head[x]; ~i; i = e[i].ne) {int v = e[i].to;if(DFN[v] == 0) {Tarjan(v);LOW[x] = min(LOW[x],LOW[v]);}else if(vis[v]) {LOW[x] = min(LOW[x],DFN[v]);}		}if(LOW[x] == DFN[x]) {scc++;while(1) {int tmp = sk[index];index--;vis[tmp]=0;col[tmp] = scc;if(tmp == x) break;}				}	
}
void init() {scc=clk=tot=TOT=index=0;for(int i = 1; i<=n; i++) {head[i] = HEAD[i] = -1;f[i]=i;vis[i] = DFN[i] = LOW[i] = in[i] = out[i] = 0;}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) ok[i][j]=0;}
}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);		init();for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);add(a,b);}for(int i = 1; i<=n; i++) {if(DFN[i] == 0) Tarjan(i);}for(int a,b,i = 1; i<=tot; i++) {a=col[e[i].fr],b=col[e[i].to];if(a==b) continue;if(ok[a][b]) continue;ok[a][b]=1;merge(a,b);in[b]++;out[a]++;}int ans = 0;int flag = 0,ru=0,chu=0;for(int i = 1; i<=scc; i++) {if(f[i] == i) ans++;if(in[i]==out[i]) continue;if(in[i]-out[i]==1) ru++;else if(out[i]-in[i]==1) chu++;else {flag=1; break;}}	if(ans > 1 || flag == 1 || ru!=chu || ru>1 || chu>1  ) puts("No");else puts("Yes");}return 0 ;
}

但是其实这样是不对的:

1
3 3
3 1
1 2
3 2

这组样例就过不了,所以不能简单判断一个有向图欧拉通路。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《TCP/IP详解》学习笔记(三):IP协议、ARP协议

把这三个协议放到一起学习是因为这三个协议处于同一层,ARP 协议用来找到目标主机的 Ethernet 网卡 Mac 地址,IP 则承载要发 送的消息。数据链路层可以从 ARP 得到数据的传送信息,而从 IP 得到要传输的数据信息。 IP 协议 IP 协议是 TCP/IP 协议的核心,所有的 TCP,UDP,IMCP,IGCP…

光与夜之恋服务器维护中,光与夜之恋7月16日停服维护说明 维护详情一览

光与夜之恋7月16日停服维护说明维护详情一览。光与夜之恋7月16日停服维护更新了哪些内容?我们去了解一下。【7月16日停服维护说明】亲爱的设计师&#xff1a;为了给设计师们提供更好的游戏体验&#xff0c;光启市将于7月16日(周五)00:00进行预计5小时的停服维护&#xff0c;可…

10.Partial Dependence Plots

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何创建和解释部分依赖图&#xff0c;这是从模型中提取洞察力的最有价值的方法之一。 What Are Partial Dependence Plots 有人抱怨机器学习模型是黑盒子。这些人会争辩说我们无法看到这些模型如何处理任何给定的数据…

springboot监控服务器信息,面试官:聊一聊SpringBoot服务监控机制

目录前言任何一个服务如果没有监控&#xff0c;那就是两眼一抹黑&#xff0c;无法知道当前服务的运行情况&#xff0c;也就无法对可能出现的异常状况进行很好的处理&#xff0c;所以对任意一个服务来说&#xff0c;监控都是必不可少的。就目前而言&#xff0c;大部分微服务应用…

0.《Apollo自动驾驶工程师技能图谱》

【新年礼物】开工第一天&#xff0c;送你一份自动驾驶工程师技能图谱&#xff01; 布道团队 Apollo开发者社区 1月 2日 AI时代到来&#xff0c;人才的缺乏是阻碍行业大步发展的主要因素之一。Apollo平台发布以来&#xff0c;我们接触到非常多的开发者他们并不是专业自动驾驶领…

【HDU - 1116】【POJ - 1386】Play on Words(判断半欧拉图,欧拉通路)

题干&#xff1a; Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large number…

11.Pipelines

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将了解如何以及为何使用管道清理建模代码。 What Are Pipelines 管道是保持数据处理和建模代码有序的简单方法。 具体来说&#xff0c;管道捆绑了预处理和建模步骤&#xff0c;因此您可以像使用单个包一样使用整个捆绑包。…

ubuntu服务器创建共享文件夹,Ubuntu samba安装创建共享目录及使用

Ubuntu samba更新了很多版本更新&#xff0c;我本人认为Ubuntu samba是很好使的文件系统&#xff0c;在此向大家推荐。如今技术不断更新&#xff0c;各种使用文件都已经淘汰。我认为还是有很不错的如Ubuntu samba值得大家来运用。一. Ubuntu samba的安装:sudo apt-get insall s…

【POJ - 2337】Catenyms(欧拉图相关,欧拉通路输出路径,tricks)

题干&#xff1a; A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms: dog.gophergopher.ratrat.tigeraloha.alohaarachnid.dog A…

12.Cross-Validation

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何使用交叉验证来更好地衡量模型性能。 What is Cross Validation 机器学习是一个迭代过程。 您将面临关于要使用的预测变量&#xff0c;要使用的模型类型&#xff0c;提供这些模型的参数等的选择。我们通过测量各…

服务器不显示u盘,服务器不读u盘启动

服务器不读u盘启动 内容精选换一换介绍使用Atlas 200 DK前需要准备的配件及开发服务器。Atlas 200 DK使用需要用户提前自购如表1所示配件。准备一个操作系统为Ubuntu X86架构的服务器&#xff0c;用途如下&#xff1a;为Atlas 200 DK制作SD卡启动盘。读卡器或者Atlas 200 DK会通…

【FZU - 2039】Pets(二分图匹配,水题)

题干&#xff1a; 有n个人&#xff0c;m条狗&#xff0c;然后会给出有一些人不喜欢一些狗就不会购买&#xff0c;问最多能卖多少狗。。 Input There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the fir…

Leetcode刷题实战(1):Two Sum

Leetcode不需要过多介绍了&#xff0c;今天一边开始刷题一边开始总结&#xff1a; 官网链接如下&#xff1a;https://leetcode.com/problemset/all/ 题1描述&#xff1a; 1Two Sum38.80%Easy Given an array of integers, return indices of the two numbers such that they…

信息服务器为什么选择在贵州,为啥云服务器在贵州

为啥云服务器在贵州 内容精选换一换当用户已在ECS服务购买GPU加速型云服务器&#xff0c;并且想在该云服务器上运行应用时&#xff0c;可以通过纳管的方式将该云服务器纳入VR云渲游平台管理。登录控制台&#xff0c;在服务列表中选择“计算 > VR云渲游平台”。在左侧导航栏&…

LeetCode刷题实战(2):Add Two Numbers

题2描述&#xff1a; 2Add Two Numbers29.10%Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a…

【BZOJ - 1305】dance跳舞(拆点网络流,建图,最大流,残留网络上跑最大流)

题干&#xff1a; 一次舞会有n个男孩和n个女孩。每首曲子开始时&#xff0c;所有男孩和女孩恰好配成n对跳交谊舞。每个男孩都不会和同一个女孩跳两首&#xff08;或更多&#xff09;舞曲。有一些男孩女孩相互喜欢&#xff0c;而其他相互不喜欢&#xff08;不会“单向喜欢”&am…

《TCP/IP详解》学习笔记(四):ICMP 协议、ping 和 Traceroute

ICMP 协议介绍 前面讲到了&#xff0c;IP 协议并不是一个可靠的协议&#xff0c;它不保证数据被成功送达&#xff0c;那么自然的&#xff0c;保证数据送达的工作应该由其他的模块来完 成。其中一个重要的模块就是 ICMP(网络控制报文)协议。 当传送 IP 数据包发生错误--比如主机…

【HDU - 1530】Maximum Clique(最大团问题,图论)

题干&#xff1a; Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex. Input Input contains multiple tests. …

Apollo自动驾驶入门课程第①讲—无人驾驶概览

目录 1. 全面了解自动驾驶主要模块 2. 了解无人车的运作方式 3. 开放式软件栈 4. 本节其他重点 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月1日 在Apollo 3.0发布的同时&#xff0c;我们面向更多对自动驾驶感兴趣的开发…

【CF - 699C】 Vacations (日程安排 dp)

题干&#xff1a; Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day…