poj 2762

Going from u to v or from v to u?
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11871 Accepted: 3018

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
题意:给定有向图,问任意两点能否连通。
思路:强连通+缩点+求解出度,入度。度大于一是有点不能连通,输出NO,否则输出Yes。
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
using namespace std;
#define max_n 5005
#define max_e 250002
#define inf 99999999

int stack[max_n],top;//栈
int isInStack[max_n];//是否在栈内
int low[max_n],dfn[max_n],tim;//点的low,dfn值;time从1开始
int node_id;
int head[max_n],s_edge;//邻接表头  s_edge从1开始
int gro_id[max_n];
int n,m;
int val[max_n];
int in[max_n],out[max_n];
struct Node
{
    int to;
    int next;
} edge[max_e];
void init()
{
    top=0;
    node_id=0;
    memset(isInStack,0,sizeof(isInStack));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    tim=0;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(head,0,sizeof(head));
    s_edge=0;
    memset(edge,0,sizeof(edge));
}
void addedge(int u,int v)
{
    s_edge++;
    edge[s_edge].to=v;
    edge[s_edge].next=head[u];
    head[u]=s_edge;
}
int min(int a,int b)
{
    if(a<b)return a;
    else return b;
}
void tarjan(int u)
{
    //low值为u或u的子树能够追溯到得最早的栈中节点的次序号
    stack[top++]=u;
    isInStack[u]=1;
    dfn[u]=++tim; //记录点u出现的记录,并放在栈中
    low[u]=tim;

    int e,v;
    for(e=head[u]; e; e=edge[e].next) //如果是叶子节点,head[u]=0,edge[e].next=0;
    {
        v=edge[e].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(isInStack[v])
            low[u]=min(low[u],dfn[v]);
    }
    int j;
    if(dfn[u]==low[u])
    {
        node_id++;
        while(j=stack[--top])
        {
            isInStack[j]=0;
            gro_id[j]=node_id;
            if(j==u)break;
        }
    }
}
void find()
{
    for(int i = 1 ; i <=n ; ++i)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
}
vector<int> vec[max_n];
int main()
{
    int a,b,t;
    cin>>t;
    while(t--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=1; i<=n; i++)
        {
            vec[i].clear();
        }
        for(int i = 0 ; i <m ; ++i)
        {
            scanf("%d%d",&a,&b);
            vec[a].push_back(b);
            addedge(a,b);
        }
        find();
        int sum=0,sum1=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<vec[i].size(); j++)//求入度为零的点
            {
                if(gro_id[i]!=gro_id[vec[i][j]])
                {
                    out[gro_id[i]]++;
                    in[gro_id[vec[i][j]]]++;
                }
            }
        }
        for(int i=1; i<=node_id; i++)
        {
            if(out[i]==0)
            sum++;
            if(in[i]==0)
            sum1++;

        }
        if(sum1>1||sum>1)
        cout<<"No"<<endl;
        else
        cout<<"Yes"<<endl;
    }
    return 0;
}
/*
1
3 3
1 2
2 3
3 1
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值