”赛码杯“ Exploration

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;
}
时间: 2024-08-21 22:34:48

”赛码杯“ Exploration的相关文章

赛码网线上PHP编译器报错

360笔试题,在赛码网提交后报错“Runtime Error:[ERROR] A Not allowed system call: runid:6660947 callid:25” 题目链接:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3980&konwledgeId=42 报错内容: 产生该错误的代码: 申明:不是因为代码逻辑原因产生错误,首先代码逻辑是按照官方给出的答案通过php实现:其次,同样的用例在本地运行(

赛码网算法:认老乡

最近在赛码网上做算法题,看到这样一道题,经过不断的学习,最后解决了.把我的思想和代码给大家分享一下~ 认老乡 题目描述大学的同学来自全国各地,对于远离家乡步入陌生大学校园的大一新生来说,碰到老乡是多么激动的一件事,于是大家都热衷于问身边的同学是否与自己同乡,来自新疆的小赛尤其热衷.但是大家都不告诉小赛他们来自哪里,只是说与谁同乡,从所给的信息中,你能告诉小赛有多少人确定是她的同乡吗? 输入每个测试实例首先包括2个整数,N(1 <= N <= 1000),M(0 <= M <= N*

赛码网的一道百度编程题

最近偶尔接触到这个赛码网,看了百度的一道编程题,于是尝试了一下,发现虽然天天写代码实现这个居然花了我好长时间,仍然没有通过全部案例.目前给的通过率是83% 题目如下: 小B最近对电子表格产生了浓厚的兴趣,她觉得电子表格很神奇,功能远比她想象的强大.她正在研究的是单元格的坐标编号,她发现表格单元一般是按列编号的,第1列编号为A,第2列为B,以此类推,第26列为Z.之后是两位字符编号的,第27列编号为AA,第28列为AB,第52列编号为AZ.之后则是三位.四位.五位--字母编号的,规则类似. 表格单

ACM 五一杭电赛码&quot;BestCoder&quot;杯中国大学生程序设计冠军赛小记

对于这项曾经热爱的竞赛,不得不说这是我最后一年参加ACM比赛了,所以要珍惜每一次比赛的机会. 五一去杭电参加了赛码"BestCoder"杯中国大学生程序设计冠军赛,去的队伍包括了今年19支World final的队伍,几乎是全国最强的46所学校各出了一个代表队,十分感谢学校给了我这个大三的老年血手这次去比赛的机会. 比赛在5.2一天内完成,上午的热身赛居然是上一场Bestcoder的原题= =.虽然我们三个人都没做过...不过我还是水水的写了前两道题. 在中午的悲惨淋雨后,下午正赛开始

赛码网 5.12周考

本来都不想写下去了,后来想想,还是写写吧!以后回过头来看看,就可以说,哦,那些时间去做这个了! 下面步入正题. 赛码网 5.12周考 题目挺难的,反正我2道题目都没有做出来,官网上也没有贴出来答案,就只好自己去找了. 1. 暴力搜索肯定是不行的,时间复杂度很高,只好找一种数个数的方法,没有想到. 题解链接:http://krydom.com/bzoj1801/ 解释的应该很详细,但是我有一点疑问, 放2个到一个空列上 与 放一个到只有一个的列上,另一个放到空列上, 这两种的转移方式 应该是不同的

贪心 赛码 1001 Movie

题目传送门 1 /* 2 贪心:官方题解: 3 首先我们考虑如何选择最左边的一个区间 4 假设最左边的区间标号是i, 那选择的另外两个区间的左端点必定要大于Ri 5 若存在i之外的j, 满足Rj<Ri, 那么另外两个区间的选择余地必定不会减少 6 因此,为了使另外两个区间有尽可能多的选择,我们选择一个右端点最小的区间作为最左边的区间是最好的 7 同理,我们选择一个左端点最大的区间作为最右边的区间,也将提供最多的选择 8 确定了这两个区间之后,只需判断是否存在一个区间位于它们中间且不交叉即可 9

递推DP 赛码 1005 Game

题目传送门 1 /* 2 递推DP:官方题解 3 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 4 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩下i − 1个人时的位置 5 (假设BrotherK所处的位置是1),然后利用之前计算出的F值判定此人是否可能获胜 6 时间复杂度为O(n3) 7 dp[i][j] 表示有i个人,j位置的人是否可能胜利.dp[1][0] = 1; cnt = sum (dp[n][i]); 8 有最

赛码网算法: 上台阶 ( python3实现 、c实现)

上台阶 题目描述 有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法?注:规定从一级到一级有0种走法. 输入输入数据首先包含一个整数n(1<=n<=100),表示测试实例的个数,然后是n行数据,每行包含一个整数m,(1<=m<=40), 表示楼梯的级数.样例输入223输出对于每个测试实例,请输出不同走法的数量.样例输出12时间限制C/C++语言:2000MS其它语言:4000MS 内存限制C/C++语言:65537KB其它语言:589825KB

赛码&quot;BestCoder&quot;杯中国大学生程序设计冠军赛1009——邻接表+并查集——Exploration

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 a