记录编号 |
583637 |
评测结果 |
AAAAAAAAAA |
题目名称 |
奶牛议会 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.108 s |
提交时间 |
2023-10-19 20:48:05 |
内存使用 |
0.58 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3+10,M = 1e4+10;
int n,m;
struct made{
int ver,nx;
}e[M];
int hd[N],tot,cnt,top,num;
int low[N],dfn[N],st[N],color[N];
bool v[N];
void add(int x,int y){
tot++;
e[tot].ver = y,e[tot].nx = hd[x],hd[x] = tot;
}
void tarjan(int x){
low[x] = dfn[x] = ++cnt;
st[++top] = x,v[x] = 1;
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(!dfn[y])tarjan(y),low[x] = min(low[x],low[y]);
else if(v[y])low[x] = min(low[x],dfn[y]);
}
if(low[x] == dfn[x]){
num++;int y = 0;
do{
y = st[top--];
v[y] = 0,color[y] = num;
}while(x != y);
}
}
void dfs(int x){
v[x] = 1;
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(!v[y])dfs(y);
}
}//直接遍历
bool check(int x){
memset(v,0,sizeof(v));
dfs(x);
for(int i = 1;i <= n;i++)
if(v[i] && v[i+n])return 0;//判断是否矛盾
return 1;
}
int main(){
freopen("cowngress.in","r",stdin);
freopen("cowngress.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++){
int x,y;char p1[2],p2[2];
scanf("%d%s%d%s",&x,p1,&y,p2);
if(p1[0] == 'Y' && p2[0] == 'Y')add(x,y+n),add(y,x+n);
else if(p1[0] == 'Y' && p2[0] == 'N')add(x,y),add(y+n,x+n);
else if(p1[0] == 'N' && p2[0] == 'Y')add(x+n,y+n),add(y,x);
else add(x+n,y),add(y+n,x);
}
for(int i = 1;i <= 2*n;i++)
if(!color[i])tarjan(i);
for(int i = 1;i <= n;i++)
if(color[i] == color[i+n]){
printf("IMPOSSIBLE\n");
return 0;
}//
for(int i = 1;i <= n;i++){
int p1 = check(i),p2 = check(i+n);//从两个点判断是否符合
if(p1 && p2)printf("?");//都
else if(p1)printf("N");
else printf("Y");
}
printf("\n");
return 0;
}