Jackiesteed

www.github.com/jackiesteed

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

花了一天弄明白了,自己代码里边写成双向了,蛋疼.

纪念一下.

模板:

View Code
#include <iostream>
#include
<fstream>
#include
<cstring>
#include
<climits>
#include
<cmath>
#include
<cstdio>
#include
<cstring>

using namespace std;

const int SIZE=3000;

typedef
struct edge
{
int t;
edge
* next;
}edge;

edge g[
1000000];
edge
* head[SIZE];

int cnt;
int top;
int stk[SIZE];
int dfn[SIZE];
int low[SIZE];
bool instk[SIZE];
int group[SIZE];
int nIndex;
int tag;
int N,M;

inline
void add_edge(int s,int t)
{
g[cnt].t
=t;
g[cnt].next
=head[s];
head[s]
=&g[cnt++];
}

void build()
{
cnt
=0;
for(int i=0;i<N*2;i++)
head[i]
=NULL;
int a1,a2,c1,c2;
for(int i=0;i<M;i++)
{
scanf(
"%d%d%d%d",&a1,&a2,&c1,&c2);
add_edge(a1
*2+c1,a2*2+(c2^1));
add_edge(a2
*2+c2,a1*2+(c1^1));

}
}

void Tarjan(int x)
{
low[x]
=dfn[x]=++nIndex;
instk[x]
=true;
stk[
++top]=x;

for(edge* p=head[x];p;p=p->next)
{
if(!dfn[p->t])
{
Tarjan(p
->t);
low[x]
=min(low[x],low[p->t]);
}
else
{
if(instk[p->t])
low[x]
=min(low[x],dfn[p->t]);
}
}
if(dfn[x]==low[x])
{
tag
++;
int tmp;
do
{
tmp
=stk[top--];
instk[tmp]
=false;
group[tmp]
=tag;

}
while(tmp!=x);
}
}

bool work()
{
nIndex
=0;
tag
=0;
top
=0;
memset(instk,
false,sizeof(instk));
memset(dfn,
0,sizeof(dfn));

for(int i=0;i<2*N;i++)
if(!dfn[i])
Tarjan(i);

for(int i=0;i<2*N;i+=2)
{
if(group[i]==group[i+1])
return false;
}
return true;

}

int main()
{
//freopen("input.txt","r",stdin);

while(scanf("%d%d",&N,&M)!=EOF && (N || M))
{
build();
if(work())
puts(
"YES");
else
puts(
"NO");
}

return 0;
}

posted on 2011-04-22 20:08  Jackiesteed  阅读(122)  评论(0编辑  收藏  举报