【hdu 3478】Catch

Link:http://acm.hdu.edu.cn/showproblem.php?pid=3478

Description

一个人从起点s出发,假设他在时间t在节点x;
则在时间t+1,他能到达x-y-z这条路径的z节点上;
也即越过一个节点.到达下一个点.
且只能这样走;
问他能不能走遍所有的点.

Solution

如果图是不联通的。那么肯定走不遍。
是联通的话,
如果是一张二分图的话.
这样的走法只能从二分图的左半部分点之间走来走去;
永远走不到右边的点;
而只要在二分图左半部分的任意两个点之间加那么一条的边.
就发现可以走到右半部分了;
然后再用二分图的规则走遍右半部分就可以了;
这样加一条边,如果更直观一点,也就是说它不是一个二分图的时候.
综上:
只要这张联通图不是二分图,就是YES,否则NO;
如果不连通输出no

NumberOf WA

0

Reviw

模型转化

Code

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5;

int n,m,s,f[N+100],color[N+100];
vector <int> g[N+100];

int ff(int x){
    if (f[x] == x) return x;
    else
        return f[x] = ff(f[x]);
}

void hebing(int x,int y){
    int r1 = ff(x),r2 = ff(y);
    if (r1!=r2) f[r1] = r2;
}

int dfs(int x,int c){
    color[x] = c;
    int len = g[x].size();
    int bo = 1;
    rep1(i,0,len-1){
        int y = g[x][i];
        if (color[y]==-1)
            bo &= dfs(y,1-c);
        else
            if (color[y]==color[x]) return 0;
    }
    return bo;
}

int main(){
    //Open();
    //Close();
    int T,kk = 0;
    ri(T);
    while (T--){
        rep1(i,1,N) g[i].clear();

        ri(n),ri(m),ri(s);s++;

        rep1(i,1,n) f[i] = i;

        rep1(i,1,m){
            int x,y;
            ri(x),ri(y);
            x++,y++;
            g[x].pb(y),g[y].pb(x);
            hebing(x,y);
        }

        bool ok = true;
        int s = ff(1);
        rep1(i,2,n)
            if (ff(i)!=s)
                ok = false;

        if (!ok) {
            os("Case ");oi(++kk);puts(": NO");
            continue;
        }

        ms(color,255);
        if (dfs(1,0)){
            os("Case ");oi(++kk);puts(": NO");
        }else{
            os("Case ");oi(++kk);puts(": YES");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626119.html