hdu4751Divide Groups【判斷二分圖】
Total Submission(s): 1904 Accepted Submission(s): 676
Problem Description
This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
After carefully planning, Tom200 announced his activity plan, one that contains two characters:
1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
Input
The input contains several test cases, terminated by EOF.
Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
Output
If divided successfully, please output "YES" in a line, else output "NO".
Sample Input
3
3 0
1 0
1 2 0
Sample Output
YES
Source
Recommend
自己好菜啊QAQ 題意:給出彼此認識的關係(不傳遞),問是否可以分成兩部分使得兩部分中都互相認識
裸的判斷二分圖,模板手誤寫錯了兩個單詞,最大的問題是最開始為標記陣列賦值的時候是單向的!加邊的時候是雙向的==
再有,難道不應該染色的時候只在for迴圈外面只賦值一次嗎?為什麼寫裡面寫外面都對啊==
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int col[200],n,x;
vector<int>G[200];
bool flag[200][200];
bool dfs(int u,int color)
{
col[u]=color;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(col[v]==color) return false;
if(col[v]==-1)
{
if(!dfs(v,!color)) return false;
}
}
return true;
}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x)&&x)
{
flag[i][x]=1;
// flag[x][i]=1;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(0==flag[i][j]||0==flag[j][i])
{
G[i].push_back(j);G[j].push_back(i);
}
}
}
bool fl=true;
memset(col,-1,sizeof(col));
for(int i=1;i<=n&&fl;i++)
{
if(col[i]==-1)
{
if(!dfs(i,0)) fl=false;
}
}
if(fl) puts("YES");
else puts("NO");
}
return 0;
}
相關文章
- [Python手撕]判斷二分圖Python
- Codeforces Round #360 (Div. 2) C DFS判斷二分圖
- 從CF1702E看二分圖判斷的兩種方法
- iOS 常用圖片格式判斷 (Swift)iOSSwift
- UML圖中時序圖的新增判斷條件時序圖
- 判斷img圖片是否載入成功
- 地圖 判斷點在區域內地圖斷點
- PbootCMS內容頁判斷有無多圖,無多圖顯示縮圖各種條件判斷和標籤boot
- 怎麼判斷dns汙染,怎麼判斷dns汙染,具體判斷方法DNS
- JS的判斷語句:判斷、迴圈JS
- python如何判斷圖片是否為空Python
- 字元判斷字元
- 如何判斷一個點在地圖上?如何判斷一個點在多邊形內?地圖
- js函式中的if判斷和a==b判斷JS函式
- 使用帶型別判斷的比較判斷型別
- 正則判斷MIME 型別是否是圖片型別
- 百度地圖電子圍欄判斷地圖
- PHP判斷檔案是否為圖片的方法PHP
- ios判斷檔案或圖片的型別iOS型別
- HDU5424Rikka with Graph II(哈密頓圖判斷)
- js資料型別判斷和陣列判斷JS資料型別陣列
- 原型判斷方法原型
- nginx 多if判斷。Nginx
- 判斷 iOS 版本iOS
- sqlldr增加判斷。SQL
- 判斷語句
- thinkphp-條件判斷-範圍判斷-range標籤PHP
- javascript如何判斷一個圖片是否載入完成JavaScript
- js如何判斷<img>圖片是否載入完畢JS
- js如何判斷img圖片是否載入完畢JS
- cv2 判斷圖片是冷還是暖
- C語言判斷素數,判斷質素演算法C語言演算法
- nginx判斷路徑是否含有某個引數做判斷Nginx
- JavaScript 判斷閏年JavaScript
- Javascript判斷空物件JavaScript物件
- CMAKE 中平臺判斷
- js判斷型別JS型別
- 判斷NaN的真假NaN