답안 #242958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
242958 2020-06-30T02:37:42 Z thebes Simurgh (IOI17_simurgh) C++14
100 / 100
210 ms 7672 KB
#include "simurgh.h"
#include <bits/stdc++.h>
#define DEBUG 1
using namespace std;

namespace output{
    void __(short x){cout<<x;}
    void __(unsigned x){cout<<x;}
    void __(int x){cout<<x;}
    void __(long long x){cout<<x;}
    void __(unsigned long long x){cout<<x;}
    void __(double x){cout<<x;}
    void __(long double x){cout<<x;}
    void __(char x){cout<<x;}
    void __(const char*x){cout<<x;}
    void __(const string&x){cout<<x;}
    void __(bool x){cout<<(x?"true":"false");}
    template<class S,class T>
    void __(const pair<S,T>&x){__(DEBUG?"(":""),__(x.first),__(DEBUG?", ":" "),__(x.second),__(DEBUG?")":"");}
    template<class T>
    void __(const vector<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class T>
    void __(const set<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class T>
    void __(const multiset<T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    template<class S,class T>
    void __(const map<S,T>&x){__(DEBUG?"{":"");bool _=0;for(const auto&v:x)__(_?DEBUG?", ":" ":""),__(v),_=1;__(DEBUG?"}":"");}
    void pr(){cout<<"\n";}
    template<class S,class... T>
    void pr(const S&a,const T&...b){__(a);if(sizeof...(b))__(' ');pr(b...);}
}

using namespace output;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,char> pic;
typedef pair<double,double> pdd;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vl;

#define pb push_back
#define eb emplace_back
#define fox(i,x,y) for(int i=(x);i<=(y);i++)
#define foxr(i,x,y) for(int i=(y);i>=(x);i--)
#define F first
#define S second

const int MN = 505;
int N, M, i, x, y, bk[MN], par[MN], id[MN], use[MN], vs[MN], val[MN*MN], dep[MN], ds[MN];
vector<pii> adj[MN], tr[MN]; vi wew, ans, tmp, a, b; pii meh[MN];

void dfs(int n,int p){
    vs[n] = 1;
    meh[n] = {1<<30, 0};
    for(auto v : adj[n]){
        if(vs[v.F]){
            if(v.F==p) continue;
            if(dep[v.F]<meh[n].F) meh[n]={dep[v.F],v.S};
        }
        else{
            dep[v.F] = dep[n]+1;
            par[v.F] = n;
            id[v.F] = v.S;
            dfs(v.F, n);
            wew.pb(v.S);
            tr[n].pb(v);
            tr[v.F].pb({n,v.S});
            if(meh[v.F].F<meh[n].F) meh[n]=meh[v.F];
        }
    }
    if(meh[n].F<dep[n]) bk[n]=meh[n].S;
}

inline void ini(){tmp.clear(); for(int _=0;_<N;_++)ds[_]=_;}
int fnd(int x){return ds[x]=x==ds[x]?x:fnd(ds[x]);}
bool uni(int x,int y){
    x=fnd(x), y=fnd(y);
    if(x!=y){ds[x]=y; return 1;}
    else return 0;
}
int treeify(){
    int res=0;
    for(auto v : wew){
        if(uni(a[v],b[v])){
            tmp.pb(v);
            res += val[v];
        }
    }
    return res;
}

vi find_roads(int n,vi u,vi v){
    N = n; M = (int)u.size();
    for(i=0;i<M;i++){
        adj[u[i]].pb({v[i],i});
        adj[v[i]].pb({u[i],i});
    }
    a=u, b=v;
    memset(bk,-1,sizeof(bk));
    dfs(0,-1);
    memset(val, -1, sizeof(val));
    int init = count_common_roads(wew);
    for(i=1;i<N;i++){
        if(bk[i]==-1) val[id[i]]=1;
        else{
            if(val[bk[i]]==-1){
                int ed = bk[i];
                x=u[ed], y=v[ed];
                if(dep[x]<dep[y]) swap(x,y);
                int dn = -1; vi acc;
                while(x!=y){
                    if(~val[id[x]]){
                        dn = id[x];
                        break;
                    }
                    acc.pb(id[x]);
                    x = par[x];
                }
                if(~dn){
                    wew.erase(find(wew.begin(),wew.end(),dn));
                    wew.pb(bk[i]);
                    int res = count_common_roads(wew);
                    wew.erase(find(wew.begin(),wew.end(),bk[i]));
                    wew.pb(dn);
                    val[bk[i]] = res-init+val[dn];
                }
                else{
                    int mx = init;
                    vi res;
                    for(auto v : acc){
                        wew.erase(find(wew.begin(),wew.end(),v));
                        wew.pb(bk[i]);
                        res.pb(count_common_roads(wew));
                        mx = max(res.back(), mx);
                        wew.pop_back();
                        wew.pb(v);
                    }
                    int fl=0;
                    for(auto v : res){
                        if(v!=mx) fl=1;
                    }
                    if(fl){
                        for(int j=0;j<(int)acc.size();j++){
                            if(res[j]!=mx) val[acc[j]]=1;
                            else val[acc[j]]=0;
                        }
                        if(init!=mx) val[bk[i]]=1;
                        else val[bk[i]]=0;
                    }
                    else{
                        for(int j=0;j<(int)acc.size();j++)
                            val[acc[j]]=0;
                        val[bk[i]]=0;
                    }
                }
            }
            if(val[id[i]]==-1){
                wew.erase(find(wew.begin(),wew.end(),id[i]));
                wew.pb(bk[i]);
                int res = count_common_roads(wew);
                wew.erase(find(wew.begin(),wew.end(),bk[i]));
                wew.pb(id[i]);
                val[id[i]] = init-res+val[bk[i]];
            }
        }
    }
    for(i=0;i<M;i++){
        if(val[i]==1) ans.pb(i);
    }
    for(i=0;i<N;i++){
        vi vec;
        for(auto v : adj[i]){
            if(val[v.S]==-1)
                vec.pb(v.S);
        }
        if(vec.size()){
            ini();
            for(auto v : vec){
                tmp.pb(v);
                uni(a[v],b[v]);
            }
            int r = treeify();
            int k = count_common_roads(tmp)-r;
            while(k--){
                int lo=0, hi=(int)vec.size()-1;
                while(lo<hi){
                    int mid=(lo+hi)>>1;
                    ini();
                    for(int j=mid+1;j<=hi;j++){
                        tmp.pb(vec[j]);
                        uni(a[vec[j]],b[vec[j]]);
                    }
                    int r = treeify();
                    if(count_common_roads(tmp)-r>0) lo=mid+1;
                    else hi=mid;
                }
                val[vec[lo]]=1;
                ans.pb(vec[lo]);
                for(int j=lo+1;j<(int)vec.size();j++)
                    val[vec[j]]=0;
                vec.erase(vec.begin()+lo,vec.end());
            }
        }
    }
    //pr(">",ans);
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB correct
2 Correct 5 ms 1408 KB correct
3 Correct 5 ms 1408 KB correct
4 Correct 5 ms 1408 KB correct
5 Correct 6 ms 1408 KB correct
6 Correct 6 ms 1280 KB correct
7 Correct 5 ms 1408 KB correct
8 Correct 5 ms 1408 KB correct
9 Correct 5 ms 1408 KB correct
10 Correct 5 ms 1312 KB correct
11 Correct 5 ms 1408 KB correct
12 Correct 5 ms 1408 KB correct
13 Correct 5 ms 1408 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB correct
2 Correct 5 ms 1408 KB correct
3 Correct 5 ms 1408 KB correct
4 Correct 5 ms 1408 KB correct
5 Correct 6 ms 1408 KB correct
6 Correct 6 ms 1280 KB correct
7 Correct 5 ms 1408 KB correct
8 Correct 5 ms 1408 KB correct
9 Correct 5 ms 1408 KB correct
10 Correct 5 ms 1312 KB correct
11 Correct 5 ms 1408 KB correct
12 Correct 5 ms 1408 KB correct
13 Correct 5 ms 1408 KB correct
14 Correct 7 ms 1408 KB correct
15 Correct 6 ms 1408 KB correct
16 Correct 6 ms 1408 KB correct
17 Correct 6 ms 1408 KB correct
18 Correct 6 ms 1408 KB correct
19 Correct 6 ms 1408 KB correct
20 Correct 6 ms 1408 KB correct
21 Correct 6 ms 1408 KB correct
22 Correct 6 ms 1408 KB correct
23 Correct 6 ms 1408 KB correct
24 Correct 6 ms 1408 KB correct
25 Correct 5 ms 1408 KB correct
26 Correct 6 ms 1408 KB correct
27 Correct 6 ms 1408 KB correct
28 Correct 6 ms 1408 KB correct
29 Correct 5 ms 1408 KB correct
30 Correct 6 ms 1408 KB correct
31 Correct 6 ms 1408 KB correct
32 Correct 6 ms 1408 KB correct
33 Correct 6 ms 1408 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB correct
2 Correct 5 ms 1408 KB correct
3 Correct 5 ms 1408 KB correct
4 Correct 5 ms 1408 KB correct
5 Correct 6 ms 1408 KB correct
6 Correct 6 ms 1280 KB correct
7 Correct 5 ms 1408 KB correct
8 Correct 5 ms 1408 KB correct
9 Correct 5 ms 1408 KB correct
10 Correct 5 ms 1312 KB correct
11 Correct 5 ms 1408 KB correct
12 Correct 5 ms 1408 KB correct
13 Correct 5 ms 1408 KB correct
14 Correct 7 ms 1408 KB correct
15 Correct 6 ms 1408 KB correct
16 Correct 6 ms 1408 KB correct
17 Correct 6 ms 1408 KB correct
18 Correct 6 ms 1408 KB correct
19 Correct 6 ms 1408 KB correct
20 Correct 6 ms 1408 KB correct
21 Correct 6 ms 1408 KB correct
22 Correct 6 ms 1408 KB correct
23 Correct 6 ms 1408 KB correct
24 Correct 6 ms 1408 KB correct
25 Correct 5 ms 1408 KB correct
26 Correct 6 ms 1408 KB correct
27 Correct 6 ms 1408 KB correct
28 Correct 6 ms 1408 KB correct
29 Correct 5 ms 1408 KB correct
30 Correct 6 ms 1408 KB correct
31 Correct 6 ms 1408 KB correct
32 Correct 6 ms 1408 KB correct
33 Correct 6 ms 1408 KB correct
34 Correct 41 ms 2816 KB correct
35 Correct 42 ms 2688 KB correct
36 Correct 38 ms 2560 KB correct
37 Correct 14 ms 1536 KB correct
38 Correct 42 ms 2816 KB correct
39 Correct 40 ms 2688 KB correct
40 Correct 35 ms 2432 KB correct
41 Correct 43 ms 2816 KB correct
42 Correct 42 ms 2816 KB correct
43 Correct 22 ms 2304 KB correct
44 Correct 20 ms 1920 KB correct
45 Correct 21 ms 2048 KB correct
46 Correct 20 ms 1920 KB correct
47 Correct 17 ms 1664 KB correct
48 Correct 8 ms 1408 KB correct
49 Correct 11 ms 1408 KB correct
50 Correct 15 ms 1664 KB correct
51 Correct 22 ms 2048 KB correct
52 Correct 21 ms 2048 KB correct
53 Correct 20 ms 1920 KB correct
54 Correct 23 ms 2304 KB correct
55 Correct 20 ms 2048 KB correct
56 Correct 20 ms 2048 KB correct
57 Correct 20 ms 2048 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB correct
2 Correct 5 ms 1408 KB correct
3 Correct 118 ms 5624 KB correct
4 Correct 185 ms 7548 KB correct
5 Correct 190 ms 7416 KB correct
6 Correct 181 ms 7548 KB correct
7 Correct 173 ms 7416 KB correct
8 Correct 175 ms 7416 KB correct
9 Correct 193 ms 7544 KB correct
10 Correct 189 ms 7544 KB correct
11 Correct 189 ms 7544 KB correct
12 Correct 203 ms 7544 KB correct
13 Correct 5 ms 1408 KB correct
14 Correct 183 ms 7416 KB correct
15 Correct 189 ms 7544 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1408 KB correct
2 Correct 5 ms 1408 KB correct
3 Correct 5 ms 1408 KB correct
4 Correct 5 ms 1408 KB correct
5 Correct 6 ms 1408 KB correct
6 Correct 6 ms 1280 KB correct
7 Correct 5 ms 1408 KB correct
8 Correct 5 ms 1408 KB correct
9 Correct 5 ms 1408 KB correct
10 Correct 5 ms 1312 KB correct
11 Correct 5 ms 1408 KB correct
12 Correct 5 ms 1408 KB correct
13 Correct 5 ms 1408 KB correct
14 Correct 7 ms 1408 KB correct
15 Correct 6 ms 1408 KB correct
16 Correct 6 ms 1408 KB correct
17 Correct 6 ms 1408 KB correct
18 Correct 6 ms 1408 KB correct
19 Correct 6 ms 1408 KB correct
20 Correct 6 ms 1408 KB correct
21 Correct 6 ms 1408 KB correct
22 Correct 6 ms 1408 KB correct
23 Correct 6 ms 1408 KB correct
24 Correct 6 ms 1408 KB correct
25 Correct 5 ms 1408 KB correct
26 Correct 6 ms 1408 KB correct
27 Correct 6 ms 1408 KB correct
28 Correct 6 ms 1408 KB correct
29 Correct 5 ms 1408 KB correct
30 Correct 6 ms 1408 KB correct
31 Correct 6 ms 1408 KB correct
32 Correct 6 ms 1408 KB correct
33 Correct 6 ms 1408 KB correct
34 Correct 41 ms 2816 KB correct
35 Correct 42 ms 2688 KB correct
36 Correct 38 ms 2560 KB correct
37 Correct 14 ms 1536 KB correct
38 Correct 42 ms 2816 KB correct
39 Correct 40 ms 2688 KB correct
40 Correct 35 ms 2432 KB correct
41 Correct 43 ms 2816 KB correct
42 Correct 42 ms 2816 KB correct
43 Correct 22 ms 2304 KB correct
44 Correct 20 ms 1920 KB correct
45 Correct 21 ms 2048 KB correct
46 Correct 20 ms 1920 KB correct
47 Correct 17 ms 1664 KB correct
48 Correct 8 ms 1408 KB correct
49 Correct 11 ms 1408 KB correct
50 Correct 15 ms 1664 KB correct
51 Correct 22 ms 2048 KB correct
52 Correct 21 ms 2048 KB correct
53 Correct 20 ms 1920 KB correct
54 Correct 23 ms 2304 KB correct
55 Correct 20 ms 2048 KB correct
56 Correct 20 ms 2048 KB correct
57 Correct 20 ms 2048 KB correct
58 Correct 5 ms 1408 KB correct
59 Correct 5 ms 1408 KB correct
60 Correct 118 ms 5624 KB correct
61 Correct 185 ms 7548 KB correct
62 Correct 190 ms 7416 KB correct
63 Correct 181 ms 7548 KB correct
64 Correct 173 ms 7416 KB correct
65 Correct 175 ms 7416 KB correct
66 Correct 193 ms 7544 KB correct
67 Correct 189 ms 7544 KB correct
68 Correct 189 ms 7544 KB correct
69 Correct 203 ms 7544 KB correct
70 Correct 5 ms 1408 KB correct
71 Correct 183 ms 7416 KB correct
72 Correct 189 ms 7544 KB correct
73 Correct 5 ms 1408 KB correct
74 Correct 201 ms 7672 KB correct
75 Correct 196 ms 7296 KB correct
76 Correct 97 ms 3712 KB correct
77 Correct 197 ms 7548 KB correct
78 Correct 210 ms 7544 KB correct
79 Correct 188 ms 7544 KB correct
80 Correct 190 ms 7420 KB correct
81 Correct 158 ms 6904 KB correct
82 Correct 188 ms 7292 KB correct
83 Correct 137 ms 4728 KB correct
84 Correct 86 ms 5496 KB correct
85 Correct 93 ms 5248 KB correct
86 Correct 72 ms 3840 KB correct
87 Correct 65 ms 3328 KB correct
88 Correct 56 ms 2688 KB correct
89 Correct 58 ms 2688 KB correct
90 Correct 52 ms 2680 KB correct
91 Correct 29 ms 1536 KB correct
92 Correct 17 ms 1408 KB correct
93 Correct 96 ms 5368 KB correct
94 Correct 76 ms 3960 KB correct
95 Correct 70 ms 3840 KB correct
96 Correct 59 ms 2560 KB correct
97 Correct 61 ms 2688 KB correct
98 Correct 68 ms 3328 KB correct
99 Correct 59 ms 2688 KB correct
100 Correct 40 ms 1792 KB correct
101 Correct 20 ms 1408 KB correct
102 Correct 77 ms 4480 KB correct
103 Correct 80 ms 4480 KB correct
104 Correct 81 ms 4480 KB correct
105 Correct 75 ms 4480 KB correct