DUT 周赛 2497 A simple problem 并查集

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2497

题意:

给你一个图,检查该图中的每一个环是否都包含了s点,若每一个环都包含的s点输出YES,否则输出NO

思路:
比赛的时候自己用并查集处理的,可是没有想到关键的处理点。。。。。

正确思路:如果存在包括s的环的话,那么肯定是从s出发又回到s的,所以我们完全没有必要将s加入集合。只把与s无关的点边进行处理,如果存在环那么这个环肯定不包含s,那么肯定输出NO,否则是就输出YES。

话说这题爆搜也能过,数据弱了点。。

并查集处理:

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <set>

#include <map>

#include <string>



#define CL(a,num) memset((a),(num),sizeof(a))

#define iabs(x)  ((x) > 0 ? (x) : -(x))

#define Min(a,b) (a) > (b)? (b):(a)

#define Max(a,b) (a) > (b)? (a):(b)



#define ll __int64

#define inf 0x7f7f7f7f

#define MOD 1073741824

#define lc l,m,rt<<1

#define rc m + 1,r,rt<<1|1

#define pi acos(-1.0)

#define test puts("<------------------->")

#define maxn 100007

#define M 500007

#define N 10007

#define K 1007

using namespace std;

//freopen("din.txt","r",stdin);





int f[N];



int find(int x)

{

    if (x != f[x])

    f[x] = find(f[x]);

    return f[x];

}

int main()

{

    int i,j;

    int x,y;

    int n,m,s;

    while (~scanf("%d%d%d",&n,&m,&s))

    {

        bool flag = false;

        for (i = 1; i <= n; ++i) f[i] = i;

        for (i = 0; i < m; ++i)

        {

            scanf("%d%d",&x,&y);

            if (x == s || y == s) continue;

            int tx = find(x);

            int ty = find(y);

            if (tx != ty) f[tx] = ty;

            else

            {

                flag = true;

            }

        }

        if (flag) printf("NO\n");

        else printf("YES\n");



    }

    return 0;



}

  

DFS:

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <cmath>

#include <queue>

#include <stack>

#include <set>

#include <map>

#include <string>



#define CL(a,num) memset((a),(num),sizeof(a))

#define iabs(x)  ((x) > 0 ? (x) : -(x))

#define Min(a,b) (a) > (b)? (b):(a)

#define Max(a,b) (a) > (b)? (a):(b)



#define ll long long

#define inf 0x7f7f7f7f

#define MOD 1073741824

#define lc l,m,rt<<1

#define rc m + 1,r,rt<<1|1

#define pi acos(-1.0)

#define test puts("<------------------->")

#define maxn 100007

#define M 1000007

#define N 10007

using namespace std;

//freopen("din.txt","r",stdin);



struct node

{

    int v;

    int next;

}g[M*2];

int head[N],ct;



int n,m,s;

bool flag,vt[N];



void add(int u,int v)

{

    g[ct].v = v;

    g[ct].next = head[u];

    head[u] = ct++;

}



void dfs(int p,int fa)

{

    int i;

    if (flag) return;

    for (i = head[p]; i != -1; i = g[i].next)

    {

        int v = g[i].v;

        if (v != fa)

        {

            if (vt[v])

            {

                if (v != s)

                {

                    flag = true;

                    break;

                }

            }

            else

            {

                vt[v] = true;

                dfs(v,p);

                vt[v] = false;

            }

        }

    }

}

int main()

{

    //freopen("din.txt","r",stdin);

    int i;

    int u,v;

    while (~scanf("%d%d%d",&n,&m,&s))

    {



        CL(head,-1); ct = 0;

        for (i = 0; i < m; ++i)

        {

            scanf("%d%d",&u,&v);

            add(u,v); add(v,u);

        }

        CL(vt,false);

        vt[s] = true;

        flag = false;

        for (i = head[s]; i != -1; i = g[i].next)

        {

            int v = g[i].v;

            if (v != s)

            {

                vt[v] = true;

                dfs(v,s);

                vt[v] = false;

            }

        }

        if (flag) printf("NO\n");

        else printf("YES\n");

    }

    return 0;

}

  

你可能感兴趣的:(simple)