[ tarjan + dfs ] poj 2762 Going from u to v or from v to u?

题目链接:

http://poj.org/problem?id=2762


Going from u to v or from v to u?

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14546   Accepted: 3837

Description

In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of their little sons go from one to the other. The son can either
go from x to y, or from y to x. Wind promised that her tasks are all possible, but she actually doesn‘t know how to decide if a task is possible. To make her life easier, Jiajia decided to choose a cave in which every pair of rooms is a possible task. Given
a cave, can you tell Jiajia whether Wind can randomly choose two rooms without worrying about anything?

Input

The first line contains a single integer T, the number of test cases. And followed T cases.

The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.

Output

The output should contain T lines. Write ‘Yes‘ if the cave has the property stated above, or ‘No‘ otherwise.

Sample Input

1
3 3
1 2
2 3
3 1

Sample Output

Yes

Source

POJ Monthly--2006.02.26,zgl & twb

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给一幅图,判断任意两点v,u是否可到达.(u->v或v->u)都可以。

解题思路:

tarjan+dfs

先求有向图强连通分量,然后缩点建图,统计入度为0的联通分量个数,超过1肯定不行。然后对搜索子树dfs,如果某一节点有超过一个儿子,则这两个儿子之间不能到达,不行。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 1100

int low[Maxn],dfn[Maxn],sc,bc,sta[Maxn];
int n,m,dep,dei[Maxn],in[Maxn];
bool iss[Maxn];
vector<vector<int> >myv;
vector<vector<int> >tree;
bool hae[Maxn][Maxn];
int ans;

void tarjan(int cur)
{
    int ne;
    low[cur]=dfn[cur]=++dep;
    sta[++sc]=cur;
    iss[cur]=true;

    for(int i=0;i<myv[cur].size();i++)
    {
        ne=myv[cur][i];
        if(!dfn[ne])
        {
            tarjan(ne);
            if(low[ne]<low[cur])
                low[cur]=low[ne];
        }
        else if(iss[ne]&&dfn[ne]<low[cur])
            low[cur]=dfn[ne];
    }
    if(low[cur]==dfn[cur])
    {
        ++bc;
        do
        {
            ne=sta[sc--];
            iss[ne]=false;
            in[ne]=bc;
        }while(ne!=cur);
    }
}
void solve()
{
    dep=sc=bc=0;
    memset(iss,false,sizeof(iss));
    memset(dfn,0,sizeof(dfn));

    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjan(i);
}
void dfs(int cur)
{
    if(ans>2)
        return ;
    int res=0;

    for(int i=0;i<tree[cur].size();i++)
    {
        int ne=tree[cur][i];
        res++;
        dfs(ne);
    }
    if(res>=2)
        ans=INF;
}
int main()
{
    //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);

   int t;

   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       myv.clear();
       myv.resize(n+1);
       memset(dei,0,sizeof(dei));
       for(int i=1;i<=m;i++)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           myv[a].push_back(b);
       }
       solve();
       tree.clear();
       tree.resize(bc+1);
       memset(hae,false,sizeof(hae));
       for(int i=1;i<=n;i++)
       {
           for(int j=0;j<myv[i].size();j++)
           {
               int ne=myv[i][j];
               if(in[i]!=in[ne])
               {
                   dei[in[ne]]++;
                   if(!hae[in[i]][in[ne]])
                   {
                       hae[in[i]][in[ne]]=true;
                       tree[in[i]].push_back(in[ne]);
                   }
               }

           }
       }
       ans=0;
       for(int i=1;i<=bc;i++)
            if(!dei[i])
            {
                ans++;
                dfs(i);
                if(ans>1)
                    break;
            }

       if(ans==1)
            printf("Yes\n");
       else
            printf("No\n");
   }
    return 0;
}
时间: 2024-10-11 22:25:09

[ tarjan + dfs ] 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)

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14469   Accepted: 3801 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

POJ 2762 tarjan缩点+并查集+度数

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494   Accepted: 4100 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

POJ 2762 tarjan缩点+拓扑

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14566   Accepted: 3846 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

poj 2762 Going from u to v or from v to u? (判断是否是弱联通图)

题意:给定一个有向图有m条单向边,判断是否任意两点都可达(a能到b或者b能到a或者互相可达),即求 弱联通分量. 算法: 先缩点求强连通分量.然后重新建图,判断新图是否是一条单链,即不能分叉,如果分叉了就会存在不可达的情况. 怎么判断是否是单链呢? 就是每次入度为0的点都只有一个,即每次队列里只有一个点. (    o(╯□╰)o.....好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去! ) #include<cstdio> #include&l

POJ 2762 Going from u to v or from v to u?(强连通分量+拓扑排序)

职务地址:id=2762">POJ 2762 先缩小点.进而推断网络拓扑结构是否每个号码1(排序我是想不出来这点的. .. ).由于假如有一层为2的话,那么从此之后这两个岔路的点就不可能从一点到还有一点的. 代码例如以下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include

POJ 2762 Going from u to v or from v to u? (有向图求单连通性)

POJ 2762 Going from u to v or from v to u? 链接:http://poj.org/problem?id=2762 题意:为了让他们的儿子变得更勇敢些,Jiajia 和Wind 将他们带到一个大洞穴中.洞穴中有n 个房间,有一些单向的通道连接某些房间.每次,Wind 选择两个房间x 和y,要求他们的一个儿子从一个房间走到另一个房间,这个儿子可以从x 走到y,也可以从y 走到x.Wind 保证她布置的任务是可以完成的,但她确实不知道如何判断一个任务是否可以完成

POJ 2762 Going from u to v or from v to u? (判断弱连通)

http://poj.org/problem?id=2762 题意:给出有向图,判断任意两个点u和v,是否可以从u到v或者从v到u. 思路: 判断图是否是弱连通的. 首先来一遍强连通缩点,重新建立新图,接下来我们在新图中找入度为0的点,入度为0的点只能有1个,如果有多个那么这些个点肯定是不能相互到达的. 如果只有一个入度为0的点,走一遍dfs判断新图是否是单链,如果有分支,那么分支上的点肯定是不能相互到达的. 1 #include<iostream> 2 #include<algorit

poj 2762 Going from u to v or from v to u? trajan+拓扑

Going from u to v or from v to u? Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors connecting some rooms. Each time, Wind choose two rooms x and y, and ask one of thei

dfs/poj 2488 A Knight&#39;s Journey

1 #include<cstdio> 2 using namespace std; 3 const int b[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; 4 int a[30][30],p,q; 5 struct 6 { 7 int x,y; 8 }step[910]; 9 10 bool dfs(int x,int y,int now) 11 { 12 if (now==p*q) return true;