답안 #436660

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
436660 2021-06-24T17:46:39 Z mat_v Simurgh (IOI17_simurgh) C++14
0 / 100
1 ms 332 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define pb push_back
#define pii pair<int,int>
#define xx first
#define yy second

using namespace std;

pii ed[500005];
int _n;

int disc[505];
bool bio[505];
vector<pii> graf[505];
pii cale[505];
vector<pii> gore[505];
int br=1;
int odg[500005];
int out[505];
int Q;
bool jbt;

void dfs(int x){
    disc[x] = br;
    bio[x] = 1;
    for(auto c:graf[x]){
        if(bio[c.xx])continue;
        cale[c.xx] = {x,c.yy};
        br++;
        dfs(c.xx);
    }
    br++;
    out[x] = br;
}

set<int> s;
int poc;

int dsu[505];
int finddsu(int x){
    if(x == dsu[x])return x;
    return dsu[x] = finddsu(dsu[x]);
}


int pitaj(){
    int pppp = s.size();
    if(pppp != _n-1)assert(false);
    if(jbt)Q++;
    if(Q > 12000)assert(false);
    vector<int> r;
    for(auto c:s)r.pb(c);
    ff(i,0,_n-1)dsu[i] = i;
    for(auto c:r){
        if(finddsu(ed[c].xx) == finddsu(ed[c].yy))assert(false);
        dsu[finddsu(ed[c].xx)] = finddsu(ed[c].yy);
    }
    return count_common_roads(r);
}

vector<pii> sad;
vector<int> ans;
vector<int> imam;

void resi(int x){
    for(auto c:graf[x]){
        if(cale[c.xx].xx == x)resi(c.xx);
    }
    if(x == 0)return;
    int i = x;
    sad.clear();
    int mks = poc;
    sad.pb({poc,cale[i].yy});
    bool da = 0;

    for(auto c:gore[i]){
        s.erase(cale[i].yy);
        s.insert(c.yy);
        int sta = pitaj();
        if(sta != poc)da = 1;
        mks = max(mks, sta);
        sad.pb({sta, c.yy});
        s.erase(c.yy);
        s.insert(cale[i].yy);
    }
    if(da){
        for(auto c:sad){
            if(c.xx == mks){
                ans.pb(c.yy);
                odg[c.yy] = 1;
            }
        }
    }
    else{
        bool ima = 0;
        for(auto c:imam){
            int mn = min(disc[ed[c].xx], disc[ed[c].yy]);
            int ms = max(disc[ed[c].xx], disc[ed[c].yy]);
            if(mn < disc[x] &&  ms > disc[x] && ms < out[x]){
                ima = 1;
                s.erase(cale[i].yy);
                s.insert(c);
                int kurac = pitaj();
                if(kurac == poc){
                    odg[cale[i].yy] = odg[c];
                }
                else if(kurac < poc)odg[cale[i].yy] = 1;
                s.erase(c);
                s.insert(cale[i].yy);
                break;
            }
        }
        if(!ima)odg[cale[i].yy] = 1;
        if(odg[cale[i].yy] == 1){
            for(auto c:sad){
                odg[c.yy] = 1;
                ans.pb(c.yy);
            }
        }
    }
    for(auto c:gore[i])imam.pb(c.yy);

}


map<pii,int> koji;
int deg[505];
int glava;
vector<int> cigani[505];
bool has[505];

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
	//std::vector<int> r(n - 1);
    int m = u.size();

    if(2 * m == (n * (n - 1))){
        _n = n;
        jbt = 1;
        ff(i,0,m - 1)ed[i] = {u[i], v[i]};
        if(n == 2){
            vector<int> r;
            r.pb(0);
            return r;
        }
        ff(i,0,m - 1)koji[{u[i],v[i]}] = koji[{v[i],u[i]}] = i;

        //cout << "SHEEESH\n";
        ff(i,0,n - 1){
            s.clear();
            ff(j,0,n - 1){
                if(i == j)continue;
                s.insert(koji[{i,j}]);
            }
            deg[i] = pitaj();

        }
        s.clear();
        int g = 0;
        ff(i,0,n - 1){
            if(deg[i] == 1)g = i;
        }
        int last = -1;
        ff(i,0,n - 1){
            if(i == g)continue;
            if(last == -1)last = i;
            else{
                s.insert(koji[{last,i}]);
            }
            last = i;
        }
        s.insert(koji[{g,(g+1)%n}]);
        int ms = pitaj();
        s.erase(koji[{g,(g+1)%n}]);
        int idx = (g+1)%n;
        ff(i,0,n - 1){
            if(i == g)continue;
            s.insert(koji[{g,i}]);
            int sta = pitaj();
            if(sta > ms){
                ms = sta;
                idx = i;
            }
            s.erase(koji[{g,i}]);
        }
        glava = idx;
        //cout << glava << "\n";
        cigani[glava].pb(g);
        ans.pb(koji[{glava,g}]);
        queue<int> q;
        ff(i,0,n - 1){
            if(deg[i] == 1)q.push(i);
        }
        while(!q.empty()){
            int sta = q.front();
            q.pop();
            if(deg[sta] == 0)break;
            //cout << sta << "\n";
            //exit(0);
            if(sta == g){
                deg[glava]--;
                if(deg[glava] == 1){
                    q.push(glava);
                }
                continue;
            }


            s.clear();
            s.insert(koji[{g,sta}]);
            vector<int> ost;
            ff(i,0,n - 1)has[i] = 0;
            has[sta] = 1;
            has[g] = 1;
            for(auto c:cigani[sta])has[c] = 1;
            ff(i,0,n - 1){
                if(!has[i])ost.pb(i);
            }
            int l = 0;
            int r = ost.size();
            int dz = ost.size();
            if(l >= r)break;
            r--;
            while(l < r){
                int mid = (l + r) / 2;
                s.clear();
                s.insert(koji[{g,sta}]);
                ff(i,l,mid){
                    s.insert(koji[{sta,ost[i]}]);
                }
                int dlt = 0;
                ff(i,0,dz - 1){
                    if(i >= l && i <= mid)continue;
                    if(ost[i] == g)continue;
                    s.insert(koji[{g,ost[i]}]);
                    if(ost[i] == glava)dlt++;
                }
                for(auto c:cigani[sta]){
                    if(c == g)continue;
                    s.insert(koji[{g,c}]);
                    if(c == glava)dlt++;
                }
                int molim = pitaj();
                molim -= dlt;
                assert(molim <= 1 && molim >= 0);
                if(molim == 1){
                    r = mid;
                }
                else l = mid + 1;
            }
            //cout << sta << " " << ost[l] << "\n";
            int tata = ost[l];
            cigani[tata].pb(sta);
            deg[tata]--;
            if(deg[tata] == 1)q.push(tata);
            ans.pb(koji[{tata,sta}]);
        }
        s.clear();
        for(auto c:ans)s.insert(c);
        //pitaj();
        return ans;
    }
    _n = n;
    ff(i,0,m - 1){
        ed[i] = {u[i],v[i]};
        graf[u[i]].pb({v[i],i});
        graf[v[i]].pb({u[i],i});
    }
    ff(i,0,n - 1)cale[i].xx = -1;
    dfs(0);
    ff(i,0,m - 1){
        int prvi = u[i];
        int drugi = v[i];
        if(cale[prvi].xx == drugi || cale[drugi].xx == prvi)continue;
        if(disc[prvi] < disc[drugi]){
            gore[drugi].pb({prvi,i});
        }
        else gore[prvi].pb({drugi,i});
    }
    ff(i,0,m - 1){
        if(cale[u[i]].xx == v[i] || cale[v[i]].xx == u[i])s.insert(i);
    }
    //cout << "kurcina\n";
    poc = pitaj();
    resi(0);
    int pp = ans.size();
    //assert(pp == n-1);
    return ans;

}
/*
4 6
0 1
0 2
0 3
1 2
1 3
2 3
0 1 5
*/

Compilation message

simurgh.cpp: In function 'int pitaj()':
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:55:5: note: in expansion of macro 'ff'
   55 |     ff(i,0,_n-1)dsu[i] = i;
      |     ^~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:141:9: note: in expansion of macro 'ff'
  141 |         ff(i,0,m - 1)ed[i] = {u[i], v[i]};
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:147:9: note: in expansion of macro 'ff'
  147 |         ff(i,0,m - 1)koji[{u[i],v[i]}] = koji[{v[i],u[i]}] = i;
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:150:9: note: in expansion of macro 'ff'
  150 |         ff(i,0,n - 1){
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:152:13: note: in expansion of macro 'ff'
  152 |             ff(j,0,n - 1){
      |             ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:161:9: note: in expansion of macro 'ff'
  161 |         ff(i,0,n - 1){
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:165:9: note: in expansion of macro 'ff'
  165 |         ff(i,0,n - 1){
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:177:9: note: in expansion of macro 'ff'
  177 |         ff(i,0,n - 1){
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:192:9: note: in expansion of macro 'ff'
  192 |         ff(i,0,n - 1){
      |         ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:213:13: note: in expansion of macro 'ff'
  213 |             ff(i,0,n - 1)has[i] = 0;
      |             ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:217:13: note: in expansion of macro 'ff'
  217 |             ff(i,0,n - 1){
      |             ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:229:17: note: in expansion of macro 'ff'
  229 |                 ff(i,l,mid){
      |                 ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:233:17: note: in expansion of macro 'ff'
  233 |                 ff(i,0,dz - 1){
      |                 ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:265:5: note: in expansion of macro 'ff'
  265 |     ff(i,0,m - 1){
      |     ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:270:5: note: in expansion of macro 'ff'
  270 |     ff(i,0,n - 1)cale[i].xx = -1;
      |     ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:272:5: note: in expansion of macro 'ff'
  272 |     ff(i,0,m - 1){
      |     ^~
simurgh.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
simurgh.cpp:281:5: note: in expansion of macro 'ff'
  281 |     ff(i,0,m - 1){
      |     ^~
simurgh.cpp:287:9: warning: unused variable 'pp' [-Wunused-variable]
  287 |     int pp = ans.size();
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Incorrect 1 ms 332 KB WA in grader: NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Incorrect 1 ms 332 KB WA in grader: NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Incorrect 1 ms 332 KB WA in grader: NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Incorrect 1 ms 332 KB WA in grader: NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Incorrect 1 ms 332 KB WA in grader: NO