zoukankan      html  css  js  c++  java
  • AtCoder Regular Contest 078D

    两边bfs,先一边找到从1到n的路径并记录下来,然后挨个标记,最后一边bfs找1能到达的点,比较一下就行了

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cassert>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #pragma comment(linker, "/STACK:1024000000,1024000000")
     
    using namespace std;
     
    const double g=10.0,eps=1e-4;
    const int N=200000+10,maxn=500+100,inf=0x3f3f3f;
     
    vector<int>v[N],p;
    bool vis[N];
    int pre[N],n,res;
    int color[N];
    void bfs(int x)
    {
        memset(vis,0,sizeof vis);
        memset(pre,-1,sizeof pre);
        queue<int>q;
        q.push(x);
        vis[x]=1;
        while(!q.empty()){
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                if(!vis[v[s][i]])
                {
                    vis[v[s][i]]=1;
                    pre[v[s][i]]=s;
                    q.push(v[s][i]);
                    if(v[s][i]==n)return ;
                }
            }
        }
    }
    int bfs1(int x)
    {
        memset(vis,0,sizeof vis);
        queue<int>q;
        q.push(x);
        vis[1]=1;
        int ans=0;
        while(!q.empty()){
            int s=q.front();
         //   cout<<s<<endl;
            ans++;
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                if(color[v[s][i]]==2||vis[v[s][i]])continue;
                q.push(v[s][i]);
                vis[v[s][i]]=1;
            }
        }
        return ans;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n;
        for(int i=2;i<=n;i++)
        {
            int a,b;
            cin>>a>>b;
            v[a].push_back(b);
            v[b].push_back(a);
        }
        bfs(1);
        int ans=0;
        for(int i=pre[n];i!=1;i=pre[i])ans++;
        ans/=2;
        memset(color,0,sizeof color);
        color[1]=1,color[n]=2;
        for(int i=pre[n];i!=1;i=pre[i])
        {
            if(ans>0)ans--,color[i]=2;
            else color[i]=1;
        }
      //  for(int i=1;i<=n;i++)cout<<color[i]<<endl;
        int res=bfs1(1);
        if(res>n-res)cout<<"Fennec"<<endl;
        else cout<<"Snuke"<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    403
    311
    401
    310
    308
    309
    307
    304
    3-1
    2-11
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/7189831.html
Copyright © 2011-2022 走看看