답안 #400756

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
400756 2021-05-08T15:50:46 Z teehandsome 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
271 ms 27972 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;

template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}

void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
    _print(H);
    if(sizeof...(T)) cerr<<",";
    else cerr<<"\"]";
    dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)

const int mxn=1003;
vector<vector<int>> adj,p;
vector<int> group[mxn];
int n;

vector<int> par;
int findp(int x) {
    while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
}
void unionset(int u,int v) {
    int U=findp(u),V=findp(v);
    par[U]=V;
}
bool solve(int root) {
    int sz=group[root].size();
    if(sz==1) return true;
    if(sz==2) {
        int u=group[root][0];
        int v=group[root][1];
        adj[u][v]=adj[v][u]=1;
        return p[u][v]!=2;
    }
    vector<bool> flag(sz,false);
    vector<int> head;
    for(int i=0;i<sz;i++) {
        if(flag[i]) continue;
        flag[i]=true;
        bool have=false;
        int u=group[root][i];
        for(int j=0;j<sz;j++) {
            if(flag[j]) continue;
            int v=group[root][j];
            if(p[u][v]==1) {
                have=true;
                adj[u][v]=adj[v][u]=1;
                flag[j]=true;
            }
        }
        head.push_back(i);
    }

    int len=head.size();
    assert(len>0);
    if(len==1) return true;
    if(len==2) return false;
//    debug(head);
    for(int i=0;i<len;i++) {
        for(int j=0;j<len;j++) {
            if(i==j) continue;
            int u=group[root][head[i]];
            int v=group[root][head[j]];
//            debug(u,v,p[u][v]);
            if(p[u][v]!=2) return false;
        }
    }
    for(int i=0;i<len;i++) {
        int u=group[root][head[i]];
        int v=group[root][head[(i+1)%len]];
        adj[u][v]=adj[v][u]=1;
    }
    return true;
}

int construct(std::vector<std::vector<int>> P) {
    p=P;
	n=p.size(); par.resize(n);
	iota(all(par),0);
//	debug(par);
	adj.resize(n,vector<int>(n,0));
	for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(p[i][j]!=0) {
                unionset(i,j);
            }
        }
	}
	for(int i=0;i<n;i++) {
        par[i]=findp(par[i]);
        group[par[i]].push_back(i);
	}
	for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(p[i][j] and findp(i)!=findp(j)) return 0;
            else if(!p[i][j] and findp(i)==findp(j)) return 0;
            else if(p[i][j]>2) return 0;
        }
	}

    vector<bool> flag(n+1,false);
    for(int i=0;i<n;i++) {
        if(flag[par[i]]) continue;
        flag[par[i]]=true;
        bool ok=solve(par[i]);
        if(!ok) return 0;
    }
    for(int i=0;i<n;i++) adj[i][i]=0;
    build(adj);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int findp(int)':
supertrees.cpp:38:5: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   38 |     while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
      |     ^~~~~
supertrees.cpp:38:51: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   38 |     while(par[x]!=x) par[x]=par[par[x]],x=par[x]; return x;
      |                                                   ^~~~~~
supertrees.cpp: In function 'bool solve(int)':
supertrees.cpp:58:14: warning: variable 'have' set but not used [-Wunused-but-set-variable]
   58 |         bool have=false;
      |              ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1356 KB Output is correct
7 Correct 254 ms 26020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1356 KB Output is correct
7 Correct 254 ms 26020 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1356 KB Output is correct
13 Correct 255 ms 25940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16028 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 6684 KB Output is correct
21 Correct 258 ms 25924 KB Output is correct
22 Correct 256 ms 25924 KB Output is correct
23 Correct 270 ms 25924 KB Output is correct
24 Correct 258 ms 26052 KB Output is correct
25 Correct 109 ms 16032 KB Output is correct
26 Correct 107 ms 16036 KB Output is correct
27 Correct 267 ms 26008 KB Output is correct
28 Correct 258 ms 26232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 11 ms 1328 KB Output is correct
9 Correct 259 ms 26148 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1356 KB Output is correct
13 Correct 262 ms 25924 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 115 ms 16040 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 65 ms 6692 KB Output is correct
22 Correct 258 ms 26028 KB Output is correct
23 Correct 251 ms 25924 KB Output is correct
24 Correct 271 ms 25936 KB Output is correct
25 Correct 112 ms 16028 KB Output is correct
26 Correct 109 ms 16024 KB Output is correct
27 Correct 255 ms 26016 KB Output is correct
28 Correct 270 ms 25928 KB Output is correct
29 Correct 111 ms 16068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 67 ms 7292 KB Output is correct
5 Correct 255 ms 27908 KB Output is correct
6 Correct 258 ms 27972 KB Output is correct
7 Correct 259 ms 27972 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 65 ms 7172 KB Output is correct
10 Correct 263 ms 27928 KB Output is correct
11 Correct 257 ms 27972 KB Output is correct
12 Correct 267 ms 27972 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 64 ms 7172 KB Output is correct
17 Correct 260 ms 27880 KB Output is correct
18 Correct 258 ms 27972 KB Output is correct
19 Correct 258 ms 27936 KB Output is correct
20 Correct 253 ms 27972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1356 KB Output is correct
7 Correct 254 ms 26020 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1356 KB Output is correct
13 Correct 255 ms 25940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16028 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 6684 KB Output is correct
21 Correct 258 ms 25924 KB Output is correct
22 Correct 256 ms 25924 KB Output is correct
23 Correct 270 ms 25924 KB Output is correct
24 Correct 258 ms 26052 KB Output is correct
25 Correct 109 ms 16032 KB Output is correct
26 Correct 107 ms 16036 KB Output is correct
27 Correct 267 ms 26008 KB Output is correct
28 Correct 258 ms 26232 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1328 KB Output is correct
37 Correct 259 ms 26148 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1356 KB Output is correct
41 Correct 262 ms 25924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 972 KB Output is correct
45 Correct 115 ms 16040 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 6692 KB Output is correct
50 Correct 258 ms 26028 KB Output is correct
51 Correct 251 ms 25924 KB Output is correct
52 Correct 271 ms 25936 KB Output is correct
53 Correct 112 ms 16028 KB Output is correct
54 Correct 109 ms 16024 KB Output is correct
55 Correct 255 ms 26016 KB Output is correct
56 Correct 270 ms 25928 KB Output is correct
57 Correct 111 ms 16068 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 6 ms 972 KB Output is correct
61 Correct 115 ms 18068 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 320 KB Output is correct
65 Correct 65 ms 7172 KB Output is correct
66 Correct 115 ms 18068 KB Output is correct
67 Correct 111 ms 17988 KB Output is correct
68 Correct 108 ms 18052 KB Output is correct
69 Correct 107 ms 17988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1356 KB Output is correct
7 Correct 254 ms 26020 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1356 KB Output is correct
13 Correct 255 ms 25940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 114 ms 16028 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 6684 KB Output is correct
21 Correct 258 ms 25924 KB Output is correct
22 Correct 256 ms 25924 KB Output is correct
23 Correct 270 ms 25924 KB Output is correct
24 Correct 258 ms 26052 KB Output is correct
25 Correct 109 ms 16032 KB Output is correct
26 Correct 107 ms 16036 KB Output is correct
27 Correct 267 ms 26008 KB Output is correct
28 Correct 258 ms 26232 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1328 KB Output is correct
37 Correct 259 ms 26148 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1356 KB Output is correct
41 Correct 262 ms 25924 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 972 KB Output is correct
45 Correct 115 ms 16040 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 6692 KB Output is correct
50 Correct 258 ms 26028 KB Output is correct
51 Correct 251 ms 25924 KB Output is correct
52 Correct 271 ms 25936 KB Output is correct
53 Correct 112 ms 16028 KB Output is correct
54 Correct 109 ms 16024 KB Output is correct
55 Correct 255 ms 26016 KB Output is correct
56 Correct 270 ms 25928 KB Output is correct
57 Correct 111 ms 16068 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 67 ms 7292 KB Output is correct
62 Correct 255 ms 27908 KB Output is correct
63 Correct 258 ms 27972 KB Output is correct
64 Correct 259 ms 27972 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 65 ms 7172 KB Output is correct
67 Correct 263 ms 27928 KB Output is correct
68 Correct 257 ms 27972 KB Output is correct
69 Correct 267 ms 27972 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 64 ms 7172 KB Output is correct
74 Correct 260 ms 27880 KB Output is correct
75 Correct 258 ms 27972 KB Output is correct
76 Correct 258 ms 27936 KB Output is correct
77 Correct 253 ms 27972 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 6 ms 972 KB Output is correct
81 Correct 115 ms 18068 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 320 KB Output is correct
85 Correct 65 ms 7172 KB Output is correct
86 Correct 115 ms 18068 KB Output is correct
87 Correct 111 ms 17988 KB Output is correct
88 Correct 108 ms 18052 KB Output is correct
89 Correct 107 ms 17988 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 6 ms 972 KB Output is correct
93 Correct 127 ms 18056 KB Output is correct
94 Correct 1 ms 208 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 42 ms 4676 KB Output is correct
98 Correct 109 ms 18072 KB Output is correct
99 Correct 111 ms 17988 KB Output is correct
100 Correct 109 ms 18052 KB Output is correct
101 Correct 109 ms 18068 KB Output is correct
102 Correct 110 ms 17988 KB Output is correct
103 Correct 115 ms 18068 KB Output is correct
104 Correct 113 ms 17972 KB Output is correct
105 Correct 118 ms 18056 KB Output is correct