首页 > 其他 > 详细

”赛码杯“ Exploration

时间:2015-05-09 20:19:37      阅读:232      评论:0      收藏:0      [点我收藏+]
Problem Description

Miceren likes exploration and he found a huge labyrinth underground!

This labyrinth has N caves and some tunnels connecting some pairs of caves.

There are two types of tunnel, one type of them can be passed in only one direction and the other can be passed in two directions. Tunnels will collapse immediately after Miceren passing them.

Now, Miceren wants to choose a cave as his start point and visit at least one other cave, finally get back to start point.

As his friend, you must help him to determine whether a start point satisfing his request exists.

Input

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

Each test case begins with three integers N, M1, M2, indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels.

The next M1 lines contain the details of the undirectional tunnels. Each line contains two integers u, v meaning that there is a undirectional tunnel between u, v. (u  v)

The next M2 lines contain the details of the directional tunnels. Each line contains integers u, v meaning that there is a directional tunnel from u to v. (u  v)

T is about 100.

1  N,M1,M2  1000000.

There may be some tunnels connect the same pair of caves.

The ratio of test cases with N > 1000 is less than 5%.

Output

For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".

Sample Input
2
5 2 1
1 2
1 2
4 5
4 2 2
1 2
2 3
4 3
4 1
Sample Output
YES
NO
Hint
If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 
这道题目的意思是N个点,M1条无向边,M2条有向边,问里面是否存在环,我的做法是直接DFS,但是第一次提交的时候是使用的vector,竟然跪了,改成了链表就OK了!
 
#include <iostream>
#include <stdio.h>
#include <vector>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std;

bool visit[1000010],flag[1000010],USE[2000010];
int x[2000010],y[2000010],NEXT[3000010],value[3000010],h[1000010];
int n,m1,m2,top;
bool success;

void addedge(int u, int v)
{
    ++top;
    NEXT[top]=h[u];
    value[top]=v;
    h[u]=top;
}

void DFS(int k)
{
    if (success) return;
    visit[k]=true;
    int q=h[k];

    while(q!=-1)
    {
        int u=value[q],to;
        if (USE[u])
        {
            q=NEXT[q];
            continue;
        }
        if (x[u]==k) to=y[u]; else to=x[u];
        if (flag[to])
        {
            success=true;
            return;
        }
        if (visit[to])
        {
            q=NEXT[q];
            continue;
        }

        flag[to]=true;
        USE[u]=true;
        DFS(to);
        flag[to]=false;
        USE[u]=false;
        q=NEXT[q];
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m1,&m2);
        top=0;
        for (int i=1;i<=n;i++)
        {
            visit[i]=false;
            h[i]=-1;
        }

        for (int i=1;i<=m1;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            addedge(x[i],i);
            addedge(y[i],i);
        }

        for (int i=m1+1;i<=m1+m2;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            addedge(x[i],i);
        }

        success=false;
        for (int i=1;i<=n;i++)
        {
            flag[i]=true;
            if (!visit[i]) DFS(i);
            flag[i]=false;
            if (success)
            {
                printf("YES\n");
                break;
            }
        }
        if (!success)
        {
            printf("NO\n");
        }
    }
    return 0;
}

 

”赛码杯“ Exploration

原文:http://www.cnblogs.com/wzb-hust/p/4490889.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!