poj 2762 Going from u to v or fr…
2013-04-23 09:24
513 查看
题意+思路 见前一博文
#include <stdio.h>
#include <string.h>
#define VM 1005
#define EM 6005
struct E
{
int
to,next;
}edge[EM];
struct E1
{
int
frm,to,next;
}edge1[EM];
int head[VM],head1[VM],indeg[VM],map[VM][VM];
int dfn[VM],low[VM],vis[VM],belong[VM],stack[VM];
int scc,cnt,top,p,n;
void addedge (int cu,int cv)
{
edge[p].to =
cv;
edge[p].next
= head[cu];
head[cu] =
p++;
}
void addedge1(int cu,int cv)
{
edge1[p].frm
= cu;
edge1[p].to
= cv;
edge1[p].next = head1[cu];
head1[cu] =
p ++;
}
void tarjan (int u)
{
int v;
dfn[u] =
low[u] = ++cnt;
stack[top++]
= u;
vis[u] =
1;
for (int i =
head[u];i != -1;i = edge[i].next)
{
v = edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = low[u] > low[v]?low[v]:low[u];
}
else if (vis[v]&&low[u]
> dfn[v])
low[u] = dfn[v];
}
if (dfn[u]
== low[u])
{
++scc;
do
{
v = stack[--top];
vis[v] = 0;
belong[v] = scc;
} while (u != v);
}
}
void sovle ()
{
int u;
scc = top =
cnt = 0;
memset
(dfn,0,sizeof(dfn));
memset
(vis,0,sizeof(vis));
for (u = 1;u
<= n;u ++)
if (!dfn[u])
tarjan(u);
}
int topo ()
{
int
u,v,i,cur,count = 0;;
for (u = 1;u
<= scc;u ++)
if (!indeg[u])
{
count ++;
cur = u;
}
if (count
> 1)
return 0;
int num =
scc;
while (num
--)
{
count = 0;
for (i = head1[cur];i != -1;i = edge1[i].next)
{
u = edge1[i].frm;
v = edge1[i].to;
//indeg[u] --;
indeg[v] --;
if (!indeg[v])
{
count ++;
cur = v;
}
}
if (count > 1)
return 0;
}
return
1;
}
int ans()
{
int
u,v,i,count = 0;
memset
(indeg,0,sizeof(indeg));
memset
(map,0,sizeof (map));
for (u = 1;u
<= n;u ++)
for (i = head[u];i != -1;i = edge[i].next)
{
v = edge[i].to;
int j,k;
j = belong[u];
k = belong[v];
if (j != k&& !map[j][k])
{
indeg[k] ++;
addedge1 (j,k);
map[j][k] = 1;
}
}
if
(topo())
return 1;
return
0;
}
int main ()
{
int
t,m,u,v;
scanf
("%d",&t);
while (t
--)
{
p = 0;
memset
(head1,-1,sizeof(head1));
memset (head,-1,sizeof(head));
scanf ("%d%d",&n,&m);
while (m --)
{
scanf ("%d%d",&u,&v);
addedge (u,v);
}
sovle ();
if (ans())
printf ("Yes\n");
else
printf("No\n");
}
return
0;
}
#include <stdio.h>
#include <string.h>
#define VM 1005
#define EM 6005
struct E
{
int
to,next;
}edge[EM];
struct E1
{
int
frm,to,next;
}edge1[EM];
int head[VM],head1[VM],indeg[VM],map[VM][VM];
int dfn[VM],low[VM],vis[VM],belong[VM],stack[VM];
int scc,cnt,top,p,n;
void addedge (int cu,int cv)
{
edge[p].to =
cv;
edge[p].next
= head[cu];
head[cu] =
p++;
}
void addedge1(int cu,int cv)
{
edge1[p].frm
= cu;
edge1[p].to
= cv;
edge1[p].next = head1[cu];
head1[cu] =
p ++;
}
void tarjan (int u)
{
int v;
dfn[u] =
low[u] = ++cnt;
stack[top++]
= u;
vis[u] =
1;
for (int i =
head[u];i != -1;i = edge[i].next)
{
v = edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u] = low[u] > low[v]?low[v]:low[u];
}
else if (vis[v]&&low[u]
> dfn[v])
low[u] = dfn[v];
}
if (dfn[u]
== low[u])
{
++scc;
do
{
v = stack[--top];
vis[v] = 0;
belong[v] = scc;
} while (u != v);
}
}
void sovle ()
{
int u;
scc = top =
cnt = 0;
memset
(dfn,0,sizeof(dfn));
memset
(vis,0,sizeof(vis));
for (u = 1;u
<= n;u ++)
if (!dfn[u])
tarjan(u);
}
int topo ()
{
int
u,v,i,cur,count = 0;;
for (u = 1;u
<= scc;u ++)
if (!indeg[u])
{
count ++;
cur = u;
}
if (count
> 1)
return 0;
int num =
scc;
while (num
--)
{
count = 0;
for (i = head1[cur];i != -1;i = edge1[i].next)
{
u = edge1[i].frm;
v = edge1[i].to;
//indeg[u] --;
indeg[v] --;
if (!indeg[v])
{
count ++;
cur = v;
}
}
if (count > 1)
return 0;
}
return
1;
}
int ans()
{
int
u,v,i,count = 0;
memset
(indeg,0,sizeof(indeg));
memset
(map,0,sizeof (map));
for (u = 1;u
<= n;u ++)
for (i = head[u];i != -1;i = edge[i].next)
{
v = edge[i].to;
int j,k;
j = belong[u];
k = belong[v];
if (j != k&& !map[j][k])
{
indeg[k] ++;
addedge1 (j,k);
map[j][k] = 1;
}
}
if
(topo())
return 1;
return
0;
}
int main ()
{
int
t,m,u,v;
scanf
("%d",&t);
while (t
--)
{
p = 0;
memset
(head1,-1,sizeof(head1));
memset (head,-1,sizeof(head));
scanf ("%d%d",&n,&m);
while (m --)
{
scanf ("%d%d",&u,&v);
addedge (u,v);
}
sovle ();
if (ans())
printf ("Yes\n");
else
printf("No\n");
}
return
0;
}
相关文章推荐
- poj&nbsp;2762&nbsp;Going&nbsp;from&nbsp;u&nbsp;to&nbsp;v&nbsp;or&nbsp;fr…
- poj 2762 Going from u to v or from v to u 判断图中任意两点x,y是否总是存在x->y或者y->x
- POJ 2762--Going from u to v or from v to u?【scc缩点新建图 && 推断是否是弱连通图】
- POJ-2762 Going from u to v or from v to u? (强连通分量[Tarjan]&&(拓扑排序||树形DP))
- POJ 2762--Going from u to v or from v to u?【scc缩点新建图 && 判断是否是弱连通图】
- POJ 2762 Going from u to v or from v to u? / 强连通分量&&拓扑
- POJ2762——Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u?
- poj2762 Going from u to v or from v to u?
- poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】
- POJ 2762 Going from u to v or from v to u?(强连通分量+缩点)
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)@
- POJ 2762 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)
- poj 2762 Going from u to v or from v to u
- POJ 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang
- poj 2762 Going from u to v or from v to u?(Tarjan+拓扑排序)
- POJ 2762 Going from u to v or from v to u?
- POJ 2762 Going from u to v or from v to u?(Tarjan)
- poj 2762 Going from u to v or from v to u?