首页 > 其他分享 >HDU 3478

HDU 3478

时间:2023-08-15 17:32:30浏览次数:55  
标签:index HDU int there 3478 cross moment flag


Catch

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1389    Accepted Submission(s): 682

Problem Description
A thief is running away!
We can consider the city where he locates as an undirected graph in which nodes stand for crosses and edges stand for streets. The crosses are labeled from 0 to N–1. 
The tricky thief starts his escaping from cross S. Each moment he moves to an adjacent cross. More exactly, assume he is at cross u at the moment t. He may appear at cross v at moment t + 1 if and only if there is a street between cross u and cross v. Notice that he may not stay at the same cross in two consecutive moment.
The cops want to know if there’s some moment at which it’s possible for the thief to appear at any cross in the city.
 
Input
The input contains multiple test cases:
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given. 
For any test case, the first line contains three integers N (≤ 100 000), M (≤ 500 000), and S. N is the number of crosses. M is the number of streets and S is the index of the cross where the thief starts his escaping.
For the next M lines, there will be 2 integers u and v in each line (0 ≤ u, v < N). It means there’s an undirected street between cross u and cross v.


Output
For each test case, output one line to tell if there’s a moment that it’s possible for the thief to appear at any cross. Look at the sample output for output format.


Sample Input
2
3 3 0
0 1
0 2
1 2
2 1 0
0 1
 
Sample Output
Case 1: YES

Case 2: NO

//染色 判断二分


#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct Node
{
    int v;
    int next;
}Edge[500010*2];

int pre[100010];

void add(int u,int v,int index)
{
    Edge[index].v=v;
    Edge[index].next=pre[u];
    pre[u]=index;
}
int flag[100010];
int c[100010];


int bfs(int v)
{
    queue <int> q;
    flag[v]=1;
    q.push(v);
    while(!q.empty())
    {
        int b=q.front();
        q.pop();
        for(int i=pre[b];i!=-1;i=Edge[i].next)
        {
            int e=Edge[i].v;
            if(flag[e]==flag[b])
                return 0;
            if(!flag[e])
            {
                flag[e]=flag[b]==1?2:1;
                q.push(e);
            }
        }
    }
    return 1;
}
int main()
{
    int n,m,q,cnt=1;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&q);
        int a,b,index=1;
        memset(flag,0,sizeof(flag));
        memset(pre,-1,sizeof(pre));
        memset(c,0,sizeof(c));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b,index++);
            add(b,a,index++);
            c[a]++,c[b]++;
        }
        int state=0;
        for(int i=1;i<=n;i++)
        {
            if(!flag[i]&&c[i])
            {
                if(!bfs(i))
                {
                    state=1;
                    break;
                }
            }
        }
        printf("Case %d: ",cnt++);
        if(state)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}




标签:index,HDU,int,there,3478,cross,moment,flag
From: https://blog.51cto.com/u_3936220/7091426

相关文章

  • HDU 5364
    DistributionmoneyTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):310  AcceptedSubmission(s):186ProblemDescriptionAFAwanttodistributionhermoneytosomebody.Shedividehermoneyintons......
  • HDU 5366
    ThemookjongTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):326  AcceptedSubmission(s):252ProblemDescription![](../../data/images/C613-1001-1.jpg)ZJiaQwanttobecomeastrongman,sohed......
  • HDU 2444
    TheAccomodationofStudentsTimeLimit:5000/1000MS(Java/Others)  MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):3561  AcceptedSubmission(s):1656ProblemDescriptionThereareagroupofstudents.Someofthemmayknoweachother,......
  • HDU 1698(线段树 区间更新)
    JustaHookTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):23481    AcceptedSubmission(s):11758ProblemDescriptionInthegameofDotA,Pudge’smeathookisactuallythemosthorr......
  • HDU 3829 Cat VS Dog 猫和狗(二分图)结题报告
    听学长说这道题很ex,但是思路想到的话还是挺简单的。可能是受上一道题(放置机器人)的启发,也是找互相冲突的点连线。但是并不是完全一样(废话)放置机器人那道题是找到冲突点连线后直接求最大匹配即可。这道题稍微把思路变换一下,求出最大完美匹配数\(n\)后,说明有\(n*2\)个人的喜好......
  • HDU7326 string magic(Easy Version)
    HDU7326stringmagic(EasyVersion)tag:回文自动机题目链接题意:多组样例,每组输入一字符串(长度1e5以内),输出满足下列条件的子串个数:该串由两个完全相同的回文串拼接而成做法:字符串的题目一般都比较板,洛谷的P4287可以说是这道题目的原题,我们先看看原题是怎么做的P4287双......
  • hdu7365 0 vs 1
    0vs1首先如果两端不同肯定只能直接选。两端都选不了直接失败。不妨设现在是zero在选,从左边来010101交替,如果先出现了一个00比如01010100.....10那么我们就能从这边选,因为one只能跟着我们选这边,最后会出现两边都是0的情况。从右边来同理。假如是0101010交替,那么肯定是平......
  • HDU 多校 Round #6 题解
    HDU多校Round#6题解\(\text{ByDaiRuiChen007}\)A.CountProblemLink题目大意求有多少个长度为\(n\),字符集大小为\(m\)的字符串有长度为\(n-k\)的周期。数据范围:\(n,m,k\le10^{18}\)。思路分析\(k=n\)时答案为\(m^n\),否则转为有长度为\(k\)的Border,答案......
  • ## HDU7328 Snake
    HDU7328Snaketag:容斥,生成函数题目链接题意:1到n个数,分成m组,组队元素排列顺序不同则为不同的组,且每组元素个数不能超过k,问有多少种方案。容斥做法:暂且不管排列的事情,先把问题看成求n个球,m个盒子,盒子不能为空,且每个盒子中球数不能超过k。要保证每盒球数不超过k,我们可通过容......
  • HDU 暑假多校 2023 第六场
    目录写在前面733673417345733773397338写在最后写在前面补题地址:https://acm.hdu.edu.cn/listproblem.php?vol=64,题号7336~7346。哈哈,单刷5题,我是只会做套路题的飞舞。Boc'z那个单曲太牛逼了,最喜欢的一首,但是live上唱的这首是真难绷。以下按个人向难度排序。7336显然......