Submission #1037187

# Submission time Handle Problem Language Result Execution time Memory
1037187 2024-07-28T10:10:37 Z huutuan Simurgh (IOI17_simurgh) C++14
100 / 100
530 ms 6228 KB
#include "simurgh.h"

#include <bits/stdc++.h>

using namespace std;

struct DSU{
   vector<int> lab;
   void init(int n){
      lab.assign(n, -1);
   }
   int find_set(int u){
      return lab[u]<0?u:lab[u]=find_set(lab[u]);
   }
   bool union_sets(int u, int v){
      u=find_set(u); v=find_set(v);
      if (u!=v){
         if (lab[u]>lab[v]) swap(u, v);
         lab[u]+=lab[v];
         lab[v]=u;
         return 1;
      }
      return 0;
   }
};

const int N=510;
vector<int> g[N], gg[N];
pair<int, int> edge[N*N];
int n, m, dep[N], par[N], val[N], vis[N];

void dfs(int u, int p){
   par[u]=p;
   for (int i:g[u]) if (i!=p){
      int v=edge[i].first^edge[i].second^u;
      if (dep[v]!=-1){
         if (dep[v]>dep[u]) gg[v].push_back(i);
      }else{
         dep[v]=dep[u]+1;
         dfs(v, i);
      }
   }
}

vector<int> find_roads(int _n, vector<int> _u, vector<int> _v) {
   n=_n, m=_u.size();
   for (int i=0; i<m; ++i){
      g[_u[i]].push_back(i);
      g[_v[i]].push_back(i);
      edge[i]={_u[i], _v[i]};
   }
   memset(dep, -1, sizeof dep);
   memset(val, -1, sizeof val);
   dep[0]=0;
   dfs(0, -1);
   for (int v=1; v<n; ++v){
      for (int i:gg[v]){
         memset(vis, 0, sizeof vis);
         int u=edge[i].first^edge[i].second^v;
         vector<int> v1, v2{i}, need, cycle{u};
         int x=-1;
         {
            int w=v;
            while (w!=u){
               cycle.push_back(w);
               v2.push_back(par[w]);
               vis[w]=1;
               if (val[w]==-1){
                  need.push_back(w);
               }else{
                  x=w;
               }
               w=edge[par[w]].first^edge[par[w]].second^w;
            }
         }
         for (int j=1; j<n; ++j) if (!vis[j]) v1.push_back(par[j]);
         if (need.empty()) continue;
         auto query=[&](int t){
            vector<int> vv=v1;
            for (int j:v2) if (j!=t) vv.push_back(j);
            return count_common_roads(vv);
         };
         if (x==-1){
            vector<int> vals;
            for (int i:v2){
               vals.push_back(query(i));
            }
            int all=*max_element(vals.begin(), vals.end());
            for (int i=1; i<(int)v2.size(); ++i) val[cycle[i]]=all-vals[i];
         }else{
            int all=query(par[x])+val[x];
            for (int w:need) val[w]=all-query(par[w]);
         }
      }
   }
   for (int i=1; i<n; ++i) if (val[i]==-1) val[i]=1;
   vector<int> ans;
   for (int u=0; u<n; ++u){
      vector<int> tmp; tmp.swap(g[u]);
      for (int i:tmp) if (u<(edge[i].first^edge[i].second^u)) g[u].push_back(i);
      auto query=[&](int l, int r){
         DSU dsu; dsu.init(n);
         vector<int> vv;
         for (int i=l; i<=r; ++i){
            dsu.union_sets(edge[g[u][i]].first, edge[g[u][i]].second);
            vv.push_back(g[u][i]);
         }
         int cnt=0;
         for (int i=1; i<n; ++i) if (dsu.union_sets(edge[par[i]].first, edge[par[i]].second)){
            vv.push_back(par[i]);
            cnt+=val[i];
         }
         return count_common_roads(vv)-cnt;
      };
      int t=0, cnt=query(0, (int)g[u].size()-1);
      while (cnt--){
         int l=t, r=(int)g[u].size()-1;
         while (l<=r){
            int mid=(l+r)>>1;
            if (query(l, mid)) r=mid-1;
            else l=mid+1;
         }
         ans.push_back(g[u][l]);
         t=l+1;
      }
   }
   return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 348 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 348 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 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 0 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 348 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 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 0 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 66 ms 1800 KB correct
35 Correct 59 ms 1768 KB correct
36 Correct 44 ms 1368 KB correct
37 Correct 7 ms 344 KB correct
38 Correct 64 ms 1624 KB correct
39 Correct 55 ms 1628 KB correct
40 Correct 43 ms 1372 KB correct
41 Correct 60 ms 1800 KB correct
42 Correct 59 ms 1624 KB correct
43 Correct 29 ms 1116 KB correct
44 Correct 24 ms 1080 KB correct
45 Correct 27 ms 1116 KB correct
46 Correct 21 ms 860 KB correct
47 Correct 11 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 6 ms 572 KB correct
50 Correct 11 ms 604 KB correct
51 Correct 27 ms 1116 KB correct
52 Correct 24 ms 860 KB correct
53 Correct 21 ms 860 KB correct
54 Correct 30 ms 1116 KB correct
55 Correct 39 ms 1112 KB correct
56 Correct 38 ms 1116 KB correct
57 Correct 35 ms 1116 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 239 ms 4244 KB correct
4 Correct 424 ms 6140 KB correct
5 Correct 425 ms 5968 KB correct
6 Correct 422 ms 6228 KB correct
7 Correct 420 ms 5980 KB correct
8 Correct 417 ms 6116 KB correct
9 Correct 424 ms 5968 KB correct
10 Correct 415 ms 6136 KB correct
11 Correct 417 ms 6084 KB correct
12 Correct 514 ms 5972 KB correct
13 Correct 0 ms 344 KB correct
14 Correct 431 ms 5976 KB correct
15 Correct 443 ms 5980 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 348 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 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 0 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 66 ms 1800 KB correct
35 Correct 59 ms 1768 KB correct
36 Correct 44 ms 1368 KB correct
37 Correct 7 ms 344 KB correct
38 Correct 64 ms 1624 KB correct
39 Correct 55 ms 1628 KB correct
40 Correct 43 ms 1372 KB correct
41 Correct 60 ms 1800 KB correct
42 Correct 59 ms 1624 KB correct
43 Correct 29 ms 1116 KB correct
44 Correct 24 ms 1080 KB correct
45 Correct 27 ms 1116 KB correct
46 Correct 21 ms 860 KB correct
47 Correct 11 ms 604 KB correct
48 Correct 3 ms 348 KB correct
49 Correct 6 ms 572 KB correct
50 Correct 11 ms 604 KB correct
51 Correct 27 ms 1116 KB correct
52 Correct 24 ms 860 KB correct
53 Correct 21 ms 860 KB correct
54 Correct 30 ms 1116 KB correct
55 Correct 39 ms 1112 KB correct
56 Correct 38 ms 1116 KB correct
57 Correct 35 ms 1116 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 239 ms 4244 KB correct
61 Correct 424 ms 6140 KB correct
62 Correct 425 ms 5968 KB correct
63 Correct 422 ms 6228 KB correct
64 Correct 420 ms 5980 KB correct
65 Correct 417 ms 6116 KB correct
66 Correct 424 ms 5968 KB correct
67 Correct 415 ms 6136 KB correct
68 Correct 417 ms 6084 KB correct
69 Correct 514 ms 5972 KB correct
70 Correct 0 ms 344 KB correct
71 Correct 431 ms 5976 KB correct
72 Correct 443 ms 5980 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 530 ms 6132 KB correct
75 Correct 438 ms 5980 KB correct
76 Correct 153 ms 2560 KB correct
77 Correct 426 ms 5980 KB correct
78 Correct 462 ms 5912 KB correct
79 Correct 435 ms 5980 KB correct
80 Correct 460 ms 5752 KB correct
81 Correct 372 ms 5212 KB correct
82 Correct 421 ms 5984 KB correct
83 Correct 251 ms 3420 KB correct
84 Correct 226 ms 3932 KB correct
85 Correct 182 ms 3676 KB correct
86 Correct 119 ms 2396 KB correct
87 Correct 108 ms 1884 KB correct
88 Correct 73 ms 1628 KB correct
89 Correct 68 ms 1628 KB correct
90 Correct 57 ms 1372 KB correct
91 Correct 20 ms 604 KB correct
92 Correct 12 ms 348 KB correct
93 Correct 187 ms 3924 KB correct
94 Correct 129 ms 2652 KB correct
95 Correct 116 ms 2644 KB correct
96 Correct 59 ms 1372 KB correct
97 Correct 97 ms 1628 KB correct
98 Correct 118 ms 1884 KB correct
99 Correct 106 ms 1700 KB correct
100 Correct 27 ms 604 KB correct
101 Correct 13 ms 580 KB correct
102 Correct 244 ms 3364 KB correct
103 Correct 241 ms 3376 KB correct
104 Correct 230 ms 3164 KB correct
105 Correct 231 ms 3164 KB correct