답안 #498834

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498834 2021-12-26T12:36:52 Z aihay Easter Eggs (info1cup17_eastereggs) C++14
58.4 / 100
400 ms 364 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;
}
vector<int> dfs2(int n,vector<int>v[513],int idx=1){
    vis[idx]=1;
    vector<int> ans;
    ans.pb(idx);
    for(int i=0;i<v[idx].size();i++){
        if(!vis[v[idx][i]]){
            vector<int> z=dfs2(n,v,v[idx][i]);
            for(int j=0;j<z.size();j++){
                ans.pb(z[j]);
            }
        }
    }
    return ans;
}
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);
    int x=1;
    bool bol=1;
    vector<int> cur;
    while(1){
        if(x<1||x>n)
            return n/2;
        bol=0;
        int tk[513]={0};
        vector<int> h;
        for(int i=1;i<=n;i++){
            if(pos[i])
                h.pb(i);
        }
        /*cout<<h.size()<<"\n";
        for(int i=0;i<h.size();i++){
            cout<<h[i]<<" ";
        }
        cout<<"cur: "<<cur.size()<<"\n";
        for(int i=0;i<cur.size();i++){
            cout<<cur[i]<<" ";
        }
        cout<<"\n";
        cout<<"bol: "<<bol<<"\n";*/
        if(cur.size()>=h.size()){
            if(query(cur)){
                //cout<<"cur > h\n";
                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];
                            //cout<<"deleted: "<<v[cur[cur.size()-1]][v[cur[cur.size()-1]].size()-1]<<"\n";
                            v[cur[cur.size()-1]].pop_back();
                            //cout<<"deleted: "<<v[cur[cur.size()-1]][v[cur[cur.size()-1]].size()-1]<<"\n";
                        }
                    }
                }
            }
            cur.clear();
            continue;
        }
        //cout<<"\n\n";
        if(h.size()==1){
            break;
        }
        for(int i=1;i<=n;i++){
            if(pos[i])
                vis[i]=0;
            else
                vis[i]=1;
        }
        int a=dfs2(n,v,x).size(),b=1;
        vector<int> va,vb;
        vb.pb(x);
        int zz=0;
        for(int i=0;i<v[x].size();i++){
            if(b+ch[v[x][i]]<a&&pos[v[x][i]]==1){
                //cout<<"took: "<<v[x][i]<<" and his children because "<<b<<"+"<<ch[v[x][i]]<<"<"<<a<<"-"<<ch[v[x][i]]<<"\n";
                zz++;
                memset(vis,0,sizeof vis);
                vis[x]=1;
                vector<int> z=dfs2(n,v,v[x][i]);
                for(int j=0;j<z.size();j++){
                    vb.pb(z[j]);
                    tk[z[j]]=1;
                }
                b+=ch[v[x][i]];
                a-=ch[v[x][i]];
            }
        }
        if(zz==0){
            //cout<<"a: "<<a<<" element "<<x<<" added to cur ";
            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;
                }
            }
            //cout<<"and x became "<<x<<"\n";
            continue;
        }
        for(int i=1;i<=n;i++){
            if(pos[i]==1&&!tk[i]){
                va.pb(i);
            }
        }
        /*cout<<"va: "<<va.size()<<"\n";
        for(int i=0;i<va.size();i++)
            cout<<va[i]<<" ";
        cout<<"\n";
        cout<<"vb: "<<vb.size()<<"\n";
        for(int i=0;i<vb.size();i++)
            cout<<vb[i]<<" ";
        cout<<"\n\n";*/
        if(query(vb)){
            cur.clear();
            bol=0;
            memset(pos,0,sizeof pos);
            for(int i=0;i<vb.size();i++){
                pos[vb[i]]=1;
            }
            //cout<<"zz: "<<zz<<"\n";
            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];
                        //cout<<"x became "<<x<<"\n";
                        break;
                    }
                }
            }
        }
        else if(cur.empty()||query(va)){
            cur.clear();
            bol=1;
            memset(pos,0,sizeof pos);
            for(int i=0;i<va.size();i++){
                pos[va[i]]=1;
            }
            //cout<<"zz: "<<zz<<"\n";
            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 'std::vector<int> dfs2(int, std::vector<int>*, int)':
eastereggs.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i=0;i<v[idx].size();i++){
      |                 ~^~~~~~~~~~~~~~
eastereggs.cpp:27:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |             for(int j=0;j<z.size();j++){
      |                         ~^~~~~~~~~
eastereggs.cpp: In function 'int findEgg(int, std::vector<std::pair<int, int> >)':
eastereggs.cpp:38: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]
   38 |     for(int i=0;i<edg.size();i++){
      |                 ~^~~~~~~~~~~
eastereggs.cpp:72:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |                 for(int i=0;i<cur.size();i++){
      |                             ~^~~~~~~~~~~
eastereggs.cpp:75:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |                 for(int i=0;i<h.size();i++){
      |                             ~^~~~~~~~~
eastereggs.cpp:78:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |                 for(int i=0;i<v[cur[cur.size()-1]].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 j=0;j<h.size();j++){
      |                                 ~^~~~~~~~~
eastereggs.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for(int i=0;i<v[x].size();i++){
      |                     ~^~~~~~~~~~~~
eastereggs.cpp:114:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |                 for(int j=0;j<z.size();j++){
      |                             ~^~~~~~~~~
eastereggs.cpp:126:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |             for(int i=0;i<v[x].size();i++){
      |                         ~^~~~~~~~~~~~
eastereggs.cpp:152:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |             for(int i=0;i<vb.size();i++){
      |                         ~^~~~~~~~~~
eastereggs.cpp:157:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |                 for(int i=0;i<v[x].size();i++){
      |                             ~^~~~~~~~~~~~
eastereggs.cpp:172:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |             for(int i=0;i<va.size();i++){
      |                         ~^~~~~~~~~~
eastereggs.cpp:179:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |                 for(int i=0;i<v[x].size();i++){
      |                             ~^~~~~~~~~~~~
eastereggs.cpp:190:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  190 |             for(int i=0;i<cur.size();i++){
      |                         ~^~~~~~~~~~~
eastereggs.cpp:46:10: warning: variable 'bol' set but not used [-Wunused-but-set-variable]
   46 |     bool bol=1;
      |          ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Number of queries: 4
2 Partially correct 1 ms 288 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 Correct 290 ms 356 KB Number of queries: 9
2 Partially correct 41 ms 364 KB Number of queries: 20
3 Partially correct 125 ms 328 KB Number of queries: 13
4 Partially correct 20 ms 328 KB Number of queries: 19
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3043 ms 340 KB Time limit exceeded
2 Halted 0 ms 0 KB -