답안 #998623

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998623 2024-06-14T11:37:21 Z bachhoangxuan Simurgh (IOI17_simurgh) C++17
100 / 100
123 ms 5132 KB
#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;

struct DSU{
    vector<int> par;
    DSU(int n=0){
        par.assign(n,0);
        iota(par.begin(),par.end(),0);
    }
    int findpar(int u){
        if(u!=par[u]) return par[u]=findpar(par[u]);
        return u;
    }
    bool unions(int u,int v){
        u=findpar(u);v=findpar(v);
        if(u!=v) par[v]=u;
        return (u!=v);
    }
};

std::vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
    vector<vector<int>> edge(n);
    int m=(int)U.size();
    for(int i=0;i<m;i++){
        edge[U[i]].push_back(i);
        edge[V[i]].push_back(i);
    }
    int T=0;
    vector<int> vis(n);
    vector<int> up(n,-1),d(n),f(n,1);
    vector<vector<int>> cc(n);
    function<void(int,int)> dfs = [&](int u,int p){
        vis[u]=++T;
        vector<int> nw;
        for(int id:edge[u]){
            int v=U[id]^V[id]^u;
            if(v==p) continue;
            if(!vis[v]){
                //cout << "edge " << u << ' ' << v << endl;
                nw.push_back(v);
                up[v]=id;
                d[v]=d[u]+1;
                dfs(v,u);
            }
            else if(vis[v]<vis[u]) cc[u].push_back(id);
        }
        edge[u]=nw;
    };
    dfs(0,0);
    function<void(int)> build = [&](int u){
        for(int v:edge[u]){
            d[v]=d[u]+!vis[v];
            build(v);
        }
    };
    vis.assign(n,0);
    for(int s=1;s<n;s++){
        for(int id:cc[s]){
            int v=U[id]^V[id]^s;
            if(d[s]==d[v]) continue;
            vector<bool> incycle(n);
            vector<int> cycle,other;
            {
                int u=s;
                while(u!=v){
                    incycle[u]=true;
                    cycle.push_back(u);
                    u^=U[up[u]]^V[up[u]];
                }
            }
            for(int i=1;i<n;i++) if(!incycle[i]) other.push_back(up[i]);
            auto get = [&](int x){
                vector<int> cur=other;
                for(int u:cycle) if(u!=x) cur.push_back(up[u]);
                if(x!=-1) cur.push_back(id);
                return count_common_roads(cur);
            };
            int x=-1;
            for(int u:cycle) if(vis[u]) x=u;
            if(x!=-1){
                int val=get(x);
                for(int u:cycle) if(!vis[u]) f[u]=f[x]^(val!=get(u));
            }
            else{
                int val=get(-1),sz=(int)cycle.size();
                vector<int> cnt(sz);
                for(int i=0;i<sz;i++) cnt[i]=get(cycle[i]),val=max(val,cnt[i]);
                for(int i=0;i<sz;i++) f[cycle[i]]=(cnt[i]!=val);
            }
            for(int u:cycle) vis[u]=1;
            build(0);
        }
    }
    vector<int> ans;
    //for(int i=1;i<n;i++) cout << f[i] << ' ';
    //cout << '\n';
    for(int i=0;i<n;i++) edge[i].clear();
    for(int i=0;i<m;i++){
        edge[U[i]].push_back(i);
        edge[V[i]].push_back(i);
    }
    for(int u=0;u<n;u++){
        auto query = [&](int l,int r){
            vector<int> cur;
            DSU dsu(n);
            for(int i=l;i<r;i++){
                int id=edge[u][i];
                cur.push_back(id);
                int v=u^U[id]^V[id];
                dsu.unions(u,v);
            }
            int add=0;
            for(int i=1;i<n;i++){
                int id=up[i];
                if(dsu.unions(U[id],V[id])){
                    cur.push_back(id);
                    add+=f[i];
                }
            }
            return count_common_roads(cur)-add;
        };
        vector<int> cur;
        for(int id:edge[u]){
            int v=u^U[id]^V[id];
            if(v>u) cur.push_back(id);
        }
        edge[u]=cur;
        int sz=(int)edge[u].size(),total=query(0,sz);
        int l=0,r=sz;
        while(total--){
            int s=l;
            l++;r=sz;
            while(l<r){
                int mid=(l+r)>>1;
                if(query(s,mid)) r=mid;
                else l=mid+1;
            }
            ans.push_back(edge[u][l-1]);
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 436 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 344 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 452 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 436 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 344 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 452 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 26 ms 1532 KB correct
35 Correct 24 ms 1504 KB correct
36 Correct 21 ms 1116 KB correct
37 Correct 7 ms 348 KB correct
38 Correct 25 ms 1532 KB correct
39 Correct 21 ms 1372 KB correct
40 Correct 19 ms 1112 KB correct
41 Correct 22 ms 1368 KB correct
42 Correct 23 ms 1372 KB correct
43 Correct 16 ms 1116 KB correct
44 Correct 14 ms 964 KB correct
45 Correct 15 ms 856 KB correct
46 Correct 14 ms 860 KB correct
47 Correct 12 ms 856 KB correct
48 Correct 4 ms 344 KB correct
49 Correct 6 ms 540 KB correct
50 Correct 11 ms 692 KB correct
51 Correct 16 ms 1032 KB correct
52 Correct 14 ms 972 KB correct
53 Correct 14 ms 848 KB correct
54 Correct 16 ms 1112 KB correct
55 Correct 17 ms 860 KB correct
56 Correct 17 ms 860 KB correct
57 Correct 17 ms 1044 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 73 ms 3652 KB correct
4 Correct 118 ms 4956 KB correct
5 Correct 120 ms 4952 KB correct
6 Correct 112 ms 4956 KB correct
7 Correct 99 ms 4952 KB correct
8 Correct 104 ms 4948 KB correct
9 Correct 115 ms 4952 KB correct
10 Correct 119 ms 5008 KB correct
11 Correct 117 ms 4952 KB correct
12 Correct 120 ms 4956 KB correct
13 Correct 1 ms 344 KB correct
14 Correct 108 ms 4956 KB correct
15 Correct 120 ms 4956 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 344 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 436 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 344 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 468 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 452 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 26 ms 1532 KB correct
35 Correct 24 ms 1504 KB correct
36 Correct 21 ms 1116 KB correct
37 Correct 7 ms 348 KB correct
38 Correct 25 ms 1532 KB correct
39 Correct 21 ms 1372 KB correct
40 Correct 19 ms 1112 KB correct
41 Correct 22 ms 1368 KB correct
42 Correct 23 ms 1372 KB correct
43 Correct 16 ms 1116 KB correct
44 Correct 14 ms 964 KB correct
45 Correct 15 ms 856 KB correct
46 Correct 14 ms 860 KB correct
47 Correct 12 ms 856 KB correct
48 Correct 4 ms 344 KB correct
49 Correct 6 ms 540 KB correct
50 Correct 11 ms 692 KB correct
51 Correct 16 ms 1032 KB correct
52 Correct 14 ms 972 KB correct
53 Correct 14 ms 848 KB correct
54 Correct 16 ms 1112 KB correct
55 Correct 17 ms 860 KB correct
56 Correct 17 ms 860 KB correct
57 Correct 17 ms 1044 KB correct
58 Correct 0 ms 344 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 73 ms 3652 KB correct
61 Correct 118 ms 4956 KB correct
62 Correct 120 ms 4952 KB correct
63 Correct 112 ms 4956 KB correct
64 Correct 99 ms 4952 KB correct
65 Correct 104 ms 4948 KB correct
66 Correct 115 ms 4952 KB correct
67 Correct 119 ms 5008 KB correct
68 Correct 117 ms 4952 KB correct
69 Correct 120 ms 4956 KB correct
70 Correct 1 ms 344 KB correct
71 Correct 108 ms 4956 KB correct
72 Correct 120 ms 4956 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 119 ms 4992 KB correct
75 Correct 116 ms 4956 KB correct
76 Correct 60 ms 2140 KB correct
77 Correct 119 ms 4948 KB correct
78 Correct 123 ms 5132 KB correct
79 Correct 121 ms 5112 KB correct
80 Correct 119 ms 4952 KB correct
81 Correct 100 ms 4444 KB correct
82 Correct 119 ms 4944 KB correct
83 Correct 96 ms 2908 KB correct
84 Correct 83 ms 3420 KB correct
85 Correct 78 ms 3252 KB correct
86 Correct 64 ms 2140 KB correct
87 Correct 55 ms 1884 KB correct
88 Correct 52 ms 1368 KB correct
89 Correct 49 ms 1372 KB correct
90 Correct 46 ms 1112 KB correct
91 Correct 25 ms 348 KB correct
92 Correct 19 ms 348 KB correct
93 Correct 78 ms 3160 KB correct
94 Correct 64 ms 2140 KB correct
95 Correct 65 ms 2272 KB correct
96 Correct 47 ms 1368 KB correct
97 Correct 54 ms 1368 KB correct
98 Correct 57 ms 1856 KB correct
99 Correct 50 ms 1372 KB correct
100 Correct 32 ms 604 KB correct
101 Correct 15 ms 588 KB correct
102 Correct 87 ms 2684 KB correct
103 Correct 84 ms 2652 KB correct
104 Correct 82 ms 2848 KB correct
105 Correct 82 ms 2652 KB correct