답안 #498862

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498862 2021-12-26T13:20:04 Z aihay Easter Eggs (info1cup17_eastereggs) C++14
26.4 / 100
140 ms 500 KB
#include <bits/stdc++.h>
#include "grader.h"

using namespace std;

#define pb push_back
#define f first
#define s second
int vis[513],pos[513],ch[513];
int dfs(int n,vector<int>v[513],int idx=1){
    vis[idx]=1;
    int x=1;
    for(int i=0;i<v[idx].size();i++){
        if(!vis[v[idx][i]])
            x+=dfs(n,v,v[idx][i]);
    }
    ch[idx]=x;
    return x;
}
int tk[513];
vector<int> va,vb;
int dfs2(int n,vector<int>v[513],int idx=1){
    vis[idx]=1;
    vb.pb(idx);
    tk[idx]=1;
    for(int i=0;i<v[idx].size();i++){
        if(!vis[v[idx][i]]){
            dfs2(n,v,v[idx][i]);
        }
    }
    return vb.size();
}
int findEgg (int n, vector<pair<int,int> > edg){
    memset(vis,0,sizeof vis);
    memset(pos,0,sizeof pos);
    vector<int> v[513];
    for(int i=0;i<edg.size();i++){
        v[edg[i].f].pb(edg[i].s);
        v[edg[i].s].pb(edg[i].f);
        pos[edg[i].f]=1;
        pos[edg[i].s]=1;
    }
    dfs(n,v,1);
    vector<int> cur,h,z;
    int x=1,a,b,zz,y;
    while(1){
        h.clear();
        for(int i=1;i<=n;i++){
            if(pos[i]){
                h.pb(i);
            }
        }
        /*if(cur.size()>=h.size()){
            if(query(cur)){
                x=cur[0];
                for(int i=0;i<cur.size();i++){
                    pos[cur[i]]=1;
                }
                for(int i=0;i<h.size();i++){
                    pos[h[i]]=0;
                }
                for(int i=0;i<v[cur[cur.size()-1]].size();i++){
                    for(int j=0;j<h.size();j++){
                        if(v[cur[cur.size()-1]][i]==h[j]){
                            ch[cur[cur.size()-1]]-=ch[h[j]];
                            v[cur[cur.size()-1]][i]=v[cur[cur.size()-1]][v[cur[cur.size()-1]].size()-1];
                            v[cur[cur.size()-1]].pop_back();
                        }
                    }
                }
            }
            cur.clear();
            continue;
        }*/
        if(h.size()==1){
            if(cur.size()){
                if(query(cur)){
                    x=cur[0];
                    for(int i=0;i<cur.size();i++){
                        pos[cur[i]]=1;
                    }
                    for(int i=0;i<h.size();i++){
                        pos[h[i]]=0;
                    }
                    for(int i=0;i<v[cur[cur.size()-1]].size();i++){
                        for(int j=0;j<h.size();j++){
                            if(v[cur[cur.size()-1]][i]==h[j]){
                                ch[cur[cur.size()-1]]-=ch[h[j]];
                                v[cur[cur.size()-1]][i]=v[cur[cur.size()-1]][v[cur[cur.size()-1]].size()-1];
                                v[cur[cur.size()-1]].pop_back();
                            }
                        }
                    }
                }
                cur.clear();
                continue;
            }
            break;
        }
        for(int i=1;i<=n;i++){
            if(pos[i])
                vis[i]=0;
            else
                vis[i]=1;
        }
        vb.clear();
        a=dfs2(n,v,x),b=1;
        va.clear(),vb.clear();
        vb.pb(x);
        zz=0;
        memset(tk,0,sizeof tk);
        for(int i=0;i<v[x].size();i++){
            if(b+ch[v[x][i]]<a&&pos[v[x][i]]==1){
                zz++;
                memset(vis,0,sizeof vis);
                vis[x]=1;
                y=dfs2(n,v,v[x][i]);
                b+=ch[v[x][i]];
                a-=ch[v[x][i]];
            }
        }
        if(zz==0){
            cur.pb(x);
            pos[x]=0;
            for(int i=0;i<v[x].size();i++){
                if(pos[v[x][i]]){
                    x=v[x][i];
                    break;
                }
            }
            continue;
        }
        for(int i=1;i<=n;i++){
            if(pos[i]==1&&!tk[i]){
                va.pb(i);
            }
        }
        if(query(vb)){
            cur.clear();
            memset(pos,0,sizeof pos);
            for(int i=0;i<vb.size();i++){
                pos[vb[i]]=1;
            }
            if(zz==1){
                for(int i=0;i<v[x].size();i++){
                    if(pos[v[x][i]]){
                        cur.pb(x);
                        pos[x]=0;
                        x=v[x][i];
                        break;
                    }
                }
            }
        }
        else if(cur.empty()||query(va)){
            cur.clear();
            memset(pos,0,sizeof pos);
            for(int i=0;i<va.size();i++){
                pos[va[i]]=1;
            }
            if(x!=1)
                zz++;
            if(zz==(int)(v[x].size())-1){
                for(int i=0;i<v[x].size();i++){
                    if(pos[v[x][i]]){
                        pos[x]=0;
                        x=v[x][i];
                        break;
                    }
                }
            }
        }
        else{
            memset(pos,0,sizeof pos);
            for(int i=0;i<cur.size();i++){
                pos[cur[i]]=1;
            }
            x=cur[0];
            cur.clear();
        }
    }
    return x;
}

Compilation message

eastereggs.cpp: In function 'int dfs(int, std::vector<int>*, int)':
eastereggs.cpp:13:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for(int i=0;i<v[idx].size();i++){
      |                 ~^~~~~~~~~~~~~~
eastereggs.cpp: In function 'int dfs2(int, std::vector<int>*, int)':
eastereggs.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for(int i=0;i<v[idx].size();i++){
      |                 ~^~~~~~~~~~~~~~
eastereggs.cpp: In function 'int findEgg(int, std::vector<std::pair<int, int> >)':
eastereggs.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int i=0;i<edg.size();i++){
      |                 ~^~~~~~~~~~~
eastereggs.cpp:79:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |                     for(int i=0;i<cur.size();i++){
      |                                 ~^~~~~~~~~~~
eastereggs.cpp:82:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |                     for(int i=0;i<h.size();i++){
      |                                 ~^~~~~~~~~
eastereggs.cpp:85:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |                     for(int i=0;i<v[cur[cur.size()-1]].size();i++){
      |                                 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
eastereggs.cpp:86:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |                         for(int j=0;j<h.size();j++){
      |                                     ~^~~~~~~~~
eastereggs.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |         for(int i=0;i<v[x].size();i++){
      |                     ~^~~~~~~~~~~~
eastereggs.cpp:125:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |             for(int i=0;i<v[x].size();i++){
      |                         ~^~~~~~~~~~~~
eastereggs.cpp:141:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |             for(int i=0;i<vb.size();i++){
      |                         ~^~~~~~~~~~
eastereggs.cpp:145:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |                 for(int i=0;i<v[x].size();i++){
      |                             ~^~~~~~~~~~~~
eastereggs.cpp:158:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |             for(int i=0;i<va.size();i++){
      |                         ~^~~~~~~~~~
eastereggs.cpp:164:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |                 for(int i=0;i<v[x].size();i++){
      |                             ~^~~~~~~~~~~~
eastereggs.cpp:175:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |             for(int i=0;i<cur.size();i++){
      |                         ~^~~~~~~~~~~
eastereggs.cpp:45:20: warning: variable 'y' set but not used [-Wunused-but-set-variable]
   45 |     int x=1,a,b,zz,y;
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 200 KB Number of queries: 8
2 Partially correct 1 ms 200 KB Number of queries: 6
3 Partially correct 1 ms 200 KB Number of queries: 6
4 Partially correct 2 ms 200 KB Number of queries: 6
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 460 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 140 ms 500 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -