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
昨天受凉了,上吐下泻,今天状态好差,头好痛。。。做不动题,学会其他的。
检查全部,对于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;
}
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<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;
}
相关文章推荐
- 排序4-归并排序
- TCP连接的建立和释放
- JAVA中堆,栈和常量池
- IOS图像7之IOS图片、动画框架
- CocoaPods
- C++第六章 习题
- java的反编译
- iOS didReceiveMemoryWarining
- 如何用一句最经典的话来批评犯错的美丽女性?
- iOS网络开发-打造自己的视频客户端
- ArduinoYun教程之ArduinoYun硬件介绍
- LeetCode_Unique Binary Search Trees
- debian-svn hooks
- 全球最流行的手机型号、屏幕尺寸及分辨率
- Unity-Shader Sublime 自制shader提示插件
- 【POJ 2240】 Arbitrage
- 从一个简单的ASP.NET 5站点开启.NET跨平台之旅
- 解析Json需要设置Mime
- mac os x命令行查找文件
- JavaWeb学习笔记——开发动态WEB资源(三)显示当前时间