比赛 |
2011.3.17 |
评测结果 |
AAAAAAAAAA |
题目名称 |
奶牛议会 |
最终得分 |
100 |
用户昵称 |
.Xmz |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2011-03-17 11:32:12 |
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxm=20001;
const int maxn=3001;
int n,m;
inline int op(int x)
{
return x>n ? x-n : x+n;
}
struct edge
{
int t,o;
edge *next;
}E[maxm],*V[maxn];
int eh;
inline void addedge(int a,int b)
{
E[++eh].next=V[a]; V[a]=E+eh; V[a]->t=b;
}
void init()
{
char st[11];
int t1,t2;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%s",&t1,st);
if (st[0]=='N') t1+=n;
scanf("%d%s",&t2,st);
if (st[0]=='N') t2+=n;
addedge(op(t1),t2);
addedge(op(t2),t1);
}
}
int stack[maxn],dfn[maxn],low[maxn],bel[maxn],sh,bcnt,indexdfn;
bool instack[maxn];
void tarjan(int u)
{
dfn[u]=low[u]=++indexdfn;
stack[++sh]=u;
instack[u]=true;
int v;
for (edge *e=V[u];e;e=e->next)
{
v=e->t;
if (!dfn[v])
{
tarjan(v);
if (low[u]>low[v]) low[u]=low[v];
}
else if (instack[v] && low[u]>dfn[v])
low[u]=dfn[v];
}
if (dfn[u]==low[u])
{
++bcnt;
do
{
v=stack[sh--];
instack[v]=false;
bel[v]=bcnt;
}while (u!=v);
}
}
edge newE[maxm],*newV[maxn];
int neweh;
inline void addnewedge(int a,int b)
{
newE[++neweh].next=newV[a]; newV[a]=newE+neweh; newV[a]->t=b; newV[a]->o=1;
newE[++neweh].next=newV[b]; newV[b]=newE+neweh; newV[b]->t=a; newV[b]->o=2;
}
int color[maxn],tcolor[maxn];
bool dfs(int u,int o)
{
color[u]=o;
for (int i=1;i<=2*n;i++)
if (bel[i]==u)
{
if (!color[bel[op(i)]] && !dfs(bel[op(i)],3-o)) return false;
if (color[bel[op(i)]] && color[bel[op(i)]]==color[u]) return false;
}
for (edge *e=newV[u];e;e=e->next)
if (e->o==o)
{
if (!color[e->t] && !dfs(e->t,o)) return false;
if (color[e->t] && color[e->t]!=color[u]) return false;
}
return true;
}
void cul(int u)
{
memcpy(tcolor,color,sizeof(color));
if (dfs(u,1))
{
memcpy(color,tcolor,sizeof(tcolor));
if (dfs(u,2)) tcolor[u]=0;
else tcolor[u]=1;
}
else tcolor[u]=2;
memcpy(color,tcolor,sizeof(tcolor));
}
void solve()
{
for (int i=1;i<=2*n;i++)
if (!dfn[i]) tarjan(i);
for (int i=1;i<=n;i++)
if (bel[i]==bel[op(i)])
{
printf("IMPOSSIBLE\n");
return ;
}
memset(newV,0,sizeof(newV));
for (int u=1;u<=2*n;u++)
for (edge *e=V[u];e;e=e->next)
if (bel[u]!=bel[e->t])
{
addnewedge(bel[u],bel[e->t]);
}
for (int i=1;i<=bcnt;i++) cul(i);
for (int i=1;i<=n;i++)
{
if (color[bel[i]]==1) printf("Y");
else if (color[bel[i]]==2) printf("N");
else printf("?");
}
printf("\n");
}
int main()
{
freopen("cowngress.in","r",stdin);
freopen("cowngress.out","w",stdout);
init();
solve();
return 0;
}