您的位置:首页 > 其它

CF 272E Dima and Horses 染色,dfs 难度:2

2015-03-11 00:35 218 查看
http://codeforces.com/problemset/problem/272/E

把仇恨关系想象为边,

因为度只能为0,1,2,3,所以有以下几种

0,1 直接放即可

2: 有(1,1),(0,2)两种情况,第一种随便放,第二种放0那里

3:有(1,2),(0,3)两种情况,第一种放1,第二种放0那里

也就是说,怎样都有解

dfs寻找即可

不过一开始觉得这样不靠谱,因为时间复杂度很高,不过没想到过了

#include <cstdio>
#include <vector>
using namespace std;
const int maxn=3e5+5;
bool other[maxn];
vector<int> e[maxn];
int n,m;
void print(){
for(int i=1;i<=n;i++){
if(other[i])printf("1");
else printf("0");
}
puts("");
}
void judge(int s){
int o=0;
for(int i=0;i<(int)e[s].size();i++){
int t=e[s][i];
if(other[t]==other[s])o++;
}
if(o>1){
other[s]=!other[s];
for(int i=0;i<(int)e[s].size();i++){
int t=e[s][i];
if(other[t]==other[s])judge(t);
}
}
}

int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int f,t;
scanf("%d%d",&f,&t);
e[f].push_back(t);
e[t].push_back(f);
}
for(int i=1;i<=n;i++){
judge(i);
}
print();

return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: