首页 技术 正文
技术 2022年11月16日
0 收藏 326 点赞 4,149 浏览 2094 个字
 /*************************************************************************
> File Name: j.cpp
> Author: HJZ
> Mail: 2570230521@qq.com
> Created Time: 2014年08月28日 星期四 12时26分13秒
************************************************************************/ //提議:能否將一個給定的有向圖,變成兩個完全圖(任意兩點都直接相連,雙向邊) #include <queue>
#include <string>
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 105
using namespace std; int a[N], b[N];
//a, b集合初始化爲空!
int g[N][N];
int n; bool flag; bool dfs(int u, int la, int lb){
if(u>n && la+lb==n) return true;//如果a ,b集合中的元素數目恰好是n,則說明可以形成兩個完全圖!
bool flagx=true;
for(int i=; i<la && flagx; ++i)
if(!(g[a[i]][u] && g[u][a[i]]))
flagx=false;
if(flagx) a[la]=u;//如果u節點可以放入a集合中
if(flagx && dfs(u+, la+, lb)) return true;
bool flagy=true;
for(int i=; i<lb && flagy; ++i)
if(!(g[b[i]][u] && g[u][b[i]]))
flagy=false;
if(flagy) b[lb]=u;//如果u節點可以放入b集合中
if(flagy && dfs(u+, la, lb+)) return true;
return false;
} int main(){
while(scanf("%d", &n)!=EOF){
memset(g, , sizeof(g));
for(int i=; i<=n; ++i){
int v;
vis[i]=;
while(scanf("%d", &v) && v)
g[i][v]=;
}
flag=dfs(, , );
if(flag)
printf("YES\n");
else printf("NO\n");
}
return ;
}
 /*************************************************************************
> File Name: j.cpp
> Author: HJZ
> Mail: 2570230521@qq.com
> Created Time: 2014年08月28日 星期四 12时26分13秒
************************************************************************/
//思路:bfs,和判断二分图差不多,将图分成两个集合,如果a和b都有g[a][b]&&g[b][a]说明
//a和b一定在同一个集合中,如果有a,b不在一个集合中,a,c不在同一个集合中,b,c也不在同一个
//集合中,出现矛盾!也就是这个图不能分成两个完全图!
#include <queue>
#include <string>
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 105
using namespace std;
queue<int>q;
int g[N][N];
int coll[N];
int n; bool bfs(int u){
while(!q.empty()) q.pop();
q.push(u);
while(!q.empty()){
int x=q.front();
q.pop();
for(int y=; y<=n; ++y){
if(x==y || g[x][y]&&g[y][x]) continue;
if(coll[y]==-){
coll[y]=coll[x]^;
q.push(y);
}
else if(coll[y]==coll[x])
return true;
}
}
return false;
} int main(){
while(scanf("%d", &n)!=EOF){
memset(g, , sizeof(g));
for(int i=; i<=n; ++i){
int v;
while(scanf("%d", &v) && v)
g[i][v]=;
coll[i]=-;
}
int i;
for(i=; i<=n; ++i){
if(coll[i]==-){
coll[i]=;//默认是在集合0中
if(bfs(i)) break;
}
}
if(i<=n) printf("NO\n");
else printf("YES\n");
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898