Submission #839363

# Submission time Handle Problem Language Result Execution time Memory
839363 2023-08-29T20:24:51 Z epicci23 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
164 ms 24184 KB
#include "supertrees.h"
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n" 
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()


const int N = 1005;

int par[N];
int siz[N];

int find(int a)
{
  if(par[a]==a) return a;
  return par[a]=find(par[a]);
}

void unite(int a,int b)
{
  a=find(a),b=find(b);
  if(a==b) return;
  if(siz[a]>siz[b]) swap(a,b);
  siz[b]+=siz[a];
  par[a]=b;
}

int construct(vector<vector<int>> p) {
  iota(par,par+N,0LL);
  fill(siz,siz+N,1LL);
    
    int n = p[0].size();
    vector<vector<int>> g(n,vector<int>(n,0));
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        if(p[i][j]) unite(i,j);
    
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
        if(!p[i][j] && find(i)==find(j))
          return 0;
        

    vector<int> no[n];
    for(int i=0;i<n;i++) no[find(i)].pb(i);

    for(int xd=0;xd<n;xd++)
    {
      if(no[xd].size()<=1) continue;
      int m = no[xd].size();
      vector<int> zort(4,0);
      vector<int> v[m];
      for(int i=0;i<m;i++)
      {
        for(int j=i;j<m;j++)
        {
          if(i==j)
          {
            v[i].pb(i);
            continue;
          }
          int a=no[xd][i];
          int b=no[xd][j];
          if(p[a][b]==1)
          {
            v[i].pb(j);
            v[j].pb(i); 
          }
          zort[p[a][b]]=1;
        }
      }
      if(zort[2] && zort[3]) return 0;
      for(int i=0;i<m;i++) sort(all(v[i]));
      vector<int> tems;
      vector<int> vis(m,0LL); 
      for(int i=0;i<m;i++)
      {
       if(vis[i]) continue;
       tems.pb(i);
       for(int x:v[i])
       {
        if(vis[x]) return 0;
        vis[x]=1; 
        if(v[i]!=v[x]) return 0;
       }
       for(int j=1;j<sz(v[i]);j++)
       {
        int a=no[xd][v[i][j]];
        int b=no[xd][v[i][j-1]];
        g[a][b]=g[b][a]=1;
       }
      }
      int k=sz(tems);
      if(k==2 && zort[2]) return 0;
      if(k>2 && zort[2])
      {
       for(int i=1;i<k;i++)
       { 
        g[no[xd][tems[i]]][no[xd][tems[i-1]]]=1;
        g[no[xd][tems[i-1]]][no[xd][tems[i]]]=1;
       } 
       g[no[xd][tems[k-1]]][no[xd][tems[0]]]=1;
       g[no[xd][tems[0]]][no[xd][tems[k-1]]]=1;
      }
      if(zort[3] && k!=4) return 0;
      if(zort[3])
      {
       for(int i=1;i<k;i++)
       { 
        g[no[xd][tems[i]]][no[xd][tems[i-1]]]=1;
        g[no[xd][tems[i-1]]][no[xd][tems[i]]]=1;
       } 
       g[no[xd][tems[k-1]]][no[xd][tems[0]]]=1;
       g[no[xd][tems[0]]][no[xd][tems[k-1]]]=1;
       g[no[xd][tems[0]]][no[xd][tems[2]]]=1;
       g[no[xd][tems[2]]][no[xd][tems[0]]]=1;
      }
    }
  build(g);
  return 1;
}
	
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1212 KB Output is correct
7 Correct 162 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1212 KB Output is correct
7 Correct 162 ms 24184 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 164 ms 24056 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 764 KB Output is correct
17 Correct 69 ms 14084 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 38 ms 6220 KB Output is correct
21 Correct 153 ms 24068 KB Output is correct
22 Correct 155 ms 24112 KB Output is correct
23 Correct 162 ms 24016 KB Output is correct
24 Correct 148 ms 24068 KB Output is correct
25 Correct 69 ms 14080 KB Output is correct
26 Correct 63 ms 14072 KB Output is correct
27 Correct 159 ms 24004 KB Output is correct
28 Correct 147 ms 24012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 304 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 0 ms 300 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 159 ms 24012 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1180 KB Output is correct
13 Correct 149 ms 24068 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 73 ms 14068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 38 ms 6220 KB Output is correct
22 Correct 151 ms 23984 KB Output is correct
23 Correct 156 ms 24028 KB Output is correct
24 Correct 152 ms 23944 KB Output is correct
25 Correct 64 ms 14072 KB Output is correct
26 Correct 63 ms 14068 KB Output is correct
27 Correct 148 ms 23928 KB Output is correct
28 Correct 152 ms 23940 KB Output is correct
29 Correct 60 ms 14068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 38 ms 6240 KB Output is correct
5 Correct 150 ms 24056 KB Output is correct
6 Correct 155 ms 23940 KB Output is correct
7 Correct 164 ms 24008 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 38 ms 6208 KB Output is correct
10 Correct 153 ms 23948 KB Output is correct
11 Correct 151 ms 24000 KB Output is correct
12 Correct 163 ms 23944 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 304 KB Output is correct
16 Correct 42 ms 6220 KB Output is correct
17 Correct 151 ms 23948 KB Output is correct
18 Correct 151 ms 23964 KB Output is correct
19 Correct 149 ms 24020 KB Output is correct
20 Correct 151 ms 23944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1212 KB Output is correct
7 Correct 162 ms 24184 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 164 ms 24056 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 764 KB Output is correct
17 Correct 69 ms 14084 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 38 ms 6220 KB Output is correct
21 Correct 153 ms 24068 KB Output is correct
22 Correct 155 ms 24112 KB Output is correct
23 Correct 162 ms 24016 KB Output is correct
24 Correct 148 ms 24068 KB Output is correct
25 Correct 69 ms 14080 KB Output is correct
26 Correct 63 ms 14072 KB Output is correct
27 Correct 159 ms 24004 KB Output is correct
28 Correct 147 ms 24012 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 304 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 304 KB Output is correct
35 Correct 0 ms 300 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 159 ms 24012 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1180 KB Output is correct
41 Correct 149 ms 24068 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 73 ms 14068 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 38 ms 6220 KB Output is correct
50 Correct 151 ms 23984 KB Output is correct
51 Correct 156 ms 24028 KB Output is correct
52 Correct 152 ms 23944 KB Output is correct
53 Correct 64 ms 14072 KB Output is correct
54 Correct 63 ms 14068 KB Output is correct
55 Correct 148 ms 23928 KB Output is correct
56 Correct 152 ms 23940 KB Output is correct
57 Correct 60 ms 14068 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 824 KB Output is correct
61 Correct 68 ms 14028 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 38 ms 6220 KB Output is correct
66 Correct 61 ms 14100 KB Output is correct
67 Correct 62 ms 14080 KB Output is correct
68 Correct 62 ms 14072 KB Output is correct
69 Correct 70 ms 14080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1212 KB Output is correct
7 Correct 162 ms 24184 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 164 ms 24056 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 764 KB Output is correct
17 Correct 69 ms 14084 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 38 ms 6220 KB Output is correct
21 Correct 153 ms 24068 KB Output is correct
22 Correct 155 ms 24112 KB Output is correct
23 Correct 162 ms 24016 KB Output is correct
24 Correct 148 ms 24068 KB Output is correct
25 Correct 69 ms 14080 KB Output is correct
26 Correct 63 ms 14072 KB Output is correct
27 Correct 159 ms 24004 KB Output is correct
28 Correct 147 ms 24012 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 304 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 304 KB Output is correct
35 Correct 0 ms 300 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 159 ms 24012 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1180 KB Output is correct
41 Correct 149 ms 24068 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 73 ms 14068 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 38 ms 6220 KB Output is correct
50 Correct 151 ms 23984 KB Output is correct
51 Correct 156 ms 24028 KB Output is correct
52 Correct 152 ms 23944 KB Output is correct
53 Correct 64 ms 14072 KB Output is correct
54 Correct 63 ms 14068 KB Output is correct
55 Correct 148 ms 23928 KB Output is correct
56 Correct 152 ms 23940 KB Output is correct
57 Correct 60 ms 14068 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 300 KB Output is correct
61 Correct 38 ms 6240 KB Output is correct
62 Correct 150 ms 24056 KB Output is correct
63 Correct 155 ms 23940 KB Output is correct
64 Correct 164 ms 24008 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 38 ms 6208 KB Output is correct
67 Correct 153 ms 23948 KB Output is correct
68 Correct 151 ms 24000 KB Output is correct
69 Correct 163 ms 23944 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 304 KB Output is correct
73 Correct 42 ms 6220 KB Output is correct
74 Correct 151 ms 23948 KB Output is correct
75 Correct 151 ms 23964 KB Output is correct
76 Correct 149 ms 24020 KB Output is correct
77 Correct 151 ms 23944 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 824 KB Output is correct
81 Correct 68 ms 14028 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 38 ms 6220 KB Output is correct
86 Correct 61 ms 14100 KB Output is correct
87 Correct 62 ms 14080 KB Output is correct
88 Correct 62 ms 14072 KB Output is correct
89 Correct 70 ms 14080 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 300 KB Output is correct
92 Correct 3 ms 852 KB Output is correct
93 Correct 66 ms 14068 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 408 KB Output is correct
97 Correct 16 ms 3704 KB Output is correct
98 Correct 61 ms 13996 KB Output is correct
99 Correct 60 ms 14028 KB Output is correct
100 Correct 61 ms 14072 KB Output is correct
101 Correct 78 ms 14084 KB Output is correct
102 Correct 63 ms 14084 KB Output is correct
103 Correct 61 ms 14072 KB Output is correct
104 Correct 69 ms 17012 KB Output is correct
105 Correct 84 ms 14080 KB Output is correct