您的位置:首页 > 其它

UVa - 11175 - From D to E and Back

2015-06-29 15:02 302 查看
TakeanydirectedgraphDwithnverticesandmedges.YoucanmaketheLyinggraphEofDinthefollowingway.Ewillhavemvertices,
oneforeachedgeofD.Forexample,ifDhasanedgeuv,thenEwillhaveavertexcalleduv.Now,wheneverDhasedgesuvandvw,Ewill
haveanedgefromvertexuvtovertexvw.TherearenootheredgesinE.

YouwillbegivenagraphEandwillhavetodeterminewhetheritispossibleforEtobetheLyinggraphofsomedirectedgraphD.

Input
Thefirstlineofinputgivesthenumberofcases,N(N<220).Ntestcasesfollow.Eachonestartswithtwolinescontainingm(0≤m≤300)andk.Thenextklines
willeachcontainapairofvertices,xandy,meaningthatthereisanedgefromxtoyinE.Theverticesarenumberedfrom0tom-1

Output

Foreachtestcase,outputonelinecontaining"Case#x:"followedbyeither"Yes"or"No",dependingonwhetherEisavalidLyinggraphornot.NotethatDisallowedtohaveduplicateedgesandself-edges.

SampleInputOutputforSampleInput

4

2

1

01

5

0

4

3

01

21

23

3

9

01

02

12

10

20

21

00

11

22

Case#1:Yes

Case#2:Yes

Case#3:No

Case#4:Yes



昨天受凉了,上吐下泻,今天状态好差,头好痛。。。做不动题,学会其他的。

检查全部,对于E中的几点v,所有指向v的结点,这些结点必然指向同一个集合。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<string>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<stack>
#include<queue>
#include<bitset>
#include<cassert>
#include<cmath>
#include<functional>

usingnamespacestd;

intg[305][305],gt[305];
intinvg[305][305],invgt[305],to[305];
intn,m;

boolcheck(){
intj,k;
for(inti=n-1;i>=0;i--){
for(j=0;j<n;j++)to[j]=0;
for(j=invgt[i]-1;j>=0;j--){
for(k=gt[invg[i][j]]-1;k>=0;k--){
to[g[invg[i][j]][k]]++;
}
}
for(j=0;j<n;j++){
if(to[j]==0||to[j]==invgt[i]){
}
else{
returnfalse;
}
}
}
returntrue;
}

intmain()
{
ios::sync_with_stdio(false);
intkase=0;
intT;
cin>>T;
while(T--){
memset(gt,0,sizeof(gt));
memset(invgt,0,sizeof(gt));
cin>>n>>m;
intx,y;
while(m--){
cin>>x>>y;
g[x][gt[x]++]=y;
invg[y][invgt[y]++]=x;
}
boolflag=check();
cout<<"Case#"<<++kase<<":"<<(flag?"Yes\n":"No\n");
}
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: