답안 #300686

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300686 2020-09-17T11:59:28 Z Odavey 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
312 ms 26360 KB
//
// ~oisín~ C++ Template
//

#include                <bits/stdc++.h>
#include                "supertrees.h"
#define MX_N            1001
#define mp              make_pair
#define mod7            1000000007
#define modpi           314159
#define PI              3.141592653589793238
#define pb              push_back
#define FastIO          ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define All(a)          a.begin(),a.end()
#define fi              first
#define se              second
#define ll              long long int
#define ull             unsigned long long int

int kx[8]  =            {+2, +2, -2, -2, +1, +1, -1, -1};
int ky[8]  =            {+1, -1, +1, -1, +2, -2, +2, -2};
int d9x[9] =            {+1, +1, +1, +0, +0, +0, -1, -1, -1};
int d9y[9] =            {+1, +0, -1, +1, +0, -1, +1, +0, -1};
int dx4[4] =            {+0, +0, +1, -1};
int dy4[4] =            {+1, -1, +0, +0};

ll gcd(ull a, ull b){
    return (a==0)?b:gcd(b%a,a);
}

ll lcm(ull a, ull b){
    return a*(b/gcd(a,b));
}

const long long INF = 1e18;

using namespace std;

int n;

struct link{
    link* par;
    int id;
    int m_id;
};

link* find_set(link* at){
    if(at == at->par){
        return at;
    }else{
        return at->par = find_set(at->par);
    }
}

void union_set(link* A, link* B){
    link* a = find_set(A);
    link* b = find_set(B);
    if(a == b){
        return;
    }
    a->par = b;
    return;
}

vector<int> adj[MX_N];
vector<int> familes[MX_N];

link* G[MX_N];

int construct(vector<vector<int> > p){
    n = p.size();
    auto res = p;
    for(auto& v : res){
        for(int& x : v){
            x = 0;
        }
    }
    for(int i=0;i<n;++i){
        G[i] = new link;
        G[i]->id = i;
        G[i]->par = G[i];
    }
    for(int i=0;i<n;++i){
        for(int j=i+1;j<n;++j){
            if(p[i][j] == 1 || p[i][j] == 2){
                adj[i].pb(j);
                adj[j].pb(i);
                union_set(G[i], G[j]);
            }
        }
    }
    for(int i=0;i<n;++i){
        for(int j=i+1;j<n;++j){
            if(p[i][j] == 0){
                if(find_set(G[i]) == find_set(G[j])){
                    return 0;
                }
            }else if(p[i][j] == 3){
                return 0;
            }
        }
    }
    for(int i=0;i<n;++i){
        G[i]->m_id = familes[find_set(G[i])->id].size();
        familes[find_set(G[i])->id].pb(i);
    }
    for(int i=0;i<n;++i){
        G[i]->par = G[i];
    }
    for(int f=0;f<n;++f){
        if(familes[f].size() <= 1){
            continue;
        }
        vector<int>& group = familes[f];
        int m = group.size();
        for(int i=0;i<m;++i){
            for(int to : adj[group[i]]){
                if(p[group[i]][to] == 1){
                    union_set(G[group[i]], G[to]);
                }
            }
        }
        vector<int> lots[m];
        for(int i=0;i<m;++i){
            lots[find_set(G[group[i]])->m_id].pb(group[i]);
        }
        vector<int> circle;
        vector<vector<int> > chains;
        for(int i=0;i<m;++i){
            if(lots[i].size() == 0){
                continue;
            }else if(lots[i].size() == 1){
                circle.pb(lots[i][0]);
            }else{
                chains.pb(lots[i]);
            }
        }
        for(auto& chain : chains){
            for(int i=1;i<(int)chain.size();++i){
                res[chain[i]][chain[i-1]] = 1;
                res[chain[i-1]][chain[i]] = 1;
            }
        }
        for(int i=1;i<(int)chains.size();++i){
            res[chains[i][0]][chains[i-1][0]] = 1;
            res[chains[i-1][0]][chains[i][0]] = 1;
        }
        for(int i=1;i<(int)circle.size();++i){
            res[circle[i]][circle[i-1]] = 1;
            res[circle[i-1]][circle[i]] = 1;
        }
        if(circle.size() == 0){
            if(chains.size() > 1){
                res[chains[0][0]][chains[chains.size()-1][0]] = 1;
                res[chains[chains.size()-1][0]][chains[0][0]] = 1;
            }
        }else if(chains.size() == 0){
            if(circle.size() == 2){
                return 0;
            }
            res[circle[0]][circle[circle.size()-1]] = 1;
            res[circle[circle.size()-1]][circle[0]] = 1;
        }else{
            res[circle[0]][chains[0][0]] = 1;
            res[chains[0][0]][circle[0]] = 1;
            res[circle[circle.size()-1]][chains[chains.size()-1][0]] = 1;
            res[chains[chains.size()-1][0]][circle[circle.size()-1]] = 1;
        }
    }
    build(res);
    return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 278 ms 26360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 278 ms 26360 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 261 ms 22404 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 69 ms 5880 KB Output is correct
21 Correct 262 ms 22648 KB Output is correct
22 Correct 259 ms 22264 KB Output is correct
23 Correct 273 ms 24312 KB Output is correct
24 Correct 255 ms 22264 KB Output is correct
25 Correct 116 ms 12792 KB Output is correct
26 Correct 113 ms 12280 KB Output is correct
27 Correct 312 ms 25208 KB Output is correct
28 Correct 252 ms 22264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 11 ms 1280 KB Output is correct
9 Correct 257 ms 22312 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 11 ms 1536 KB Output is correct
13 Correct 269 ms 26356 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 128 ms 14968 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 63 ms 5872 KB Output is correct
22 Correct 257 ms 22664 KB Output is correct
23 Correct 258 ms 22276 KB Output is correct
24 Correct 271 ms 25348 KB Output is correct
25 Correct 114 ms 12304 KB Output is correct
26 Correct 118 ms 12808 KB Output is correct
27 Correct 259 ms 22264 KB Output is correct
28 Correct 268 ms 24240 KB Output is correct
29 Correct 122 ms 12296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 63 ms 5880 KB Output is correct
5 Correct 268 ms 22660 KB Output is correct
6 Correct 253 ms 22448 KB Output is correct
7 Correct 268 ms 24312 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 65 ms 5880 KB Output is correct
10 Correct 265 ms 22652 KB Output is correct
11 Correct 270 ms 22264 KB Output is correct
12 Correct 295 ms 25236 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 70 ms 5884 KB Output is correct
17 Correct 261 ms 22840 KB Output is correct
18 Correct 266 ms 22660 KB Output is correct
19 Correct 262 ms 22412 KB Output is correct
20 Correct 271 ms 22220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 278 ms 26360 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 261 ms 22404 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 69 ms 5880 KB Output is correct
21 Correct 262 ms 22648 KB Output is correct
22 Correct 259 ms 22264 KB Output is correct
23 Correct 273 ms 24312 KB Output is correct
24 Correct 255 ms 22264 KB Output is correct
25 Correct 116 ms 12792 KB Output is correct
26 Correct 113 ms 12280 KB Output is correct
27 Correct 312 ms 25208 KB Output is correct
28 Correct 252 ms 22264 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 11 ms 1280 KB Output is correct
37 Correct 257 ms 22312 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 269 ms 26356 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 6 ms 1024 KB Output is correct
45 Correct 128 ms 14968 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 63 ms 5872 KB Output is correct
50 Correct 257 ms 22664 KB Output is correct
51 Correct 258 ms 22276 KB Output is correct
52 Correct 271 ms 25348 KB Output is correct
53 Correct 114 ms 12304 KB Output is correct
54 Correct 118 ms 12808 KB Output is correct
55 Correct 259 ms 22264 KB Output is correct
56 Correct 268 ms 24240 KB Output is correct
57 Correct 122 ms 12296 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 6 ms 1036 KB Output is correct
61 Correct 139 ms 16760 KB Output is correct
62 Correct 0 ms 384 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 65 ms 6008 KB Output is correct
66 Correct 126 ms 12792 KB Output is correct
67 Correct 124 ms 12792 KB Output is correct
68 Correct 119 ms 12408 KB Output is correct
69 Correct 119 ms 12280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 278 ms 26360 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 512 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 261 ms 22404 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 127 ms 14968 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 69 ms 5880 KB Output is correct
21 Correct 262 ms 22648 KB Output is correct
22 Correct 259 ms 22264 KB Output is correct
23 Correct 273 ms 24312 KB Output is correct
24 Correct 255 ms 22264 KB Output is correct
25 Correct 116 ms 12792 KB Output is correct
26 Correct 113 ms 12280 KB Output is correct
27 Correct 312 ms 25208 KB Output is correct
28 Correct 252 ms 22264 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 0 ms 384 KB Output is correct
36 Correct 11 ms 1280 KB Output is correct
37 Correct 257 ms 22312 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 11 ms 1536 KB Output is correct
41 Correct 269 ms 26356 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 6 ms 1024 KB Output is correct
45 Correct 128 ms 14968 KB Output is correct
46 Correct 1 ms 384 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 63 ms 5872 KB Output is correct
50 Correct 257 ms 22664 KB Output is correct
51 Correct 258 ms 22276 KB Output is correct
52 Correct 271 ms 25348 KB Output is correct
53 Correct 114 ms 12304 KB Output is correct
54 Correct 118 ms 12808 KB Output is correct
55 Correct 259 ms 22264 KB Output is correct
56 Correct 268 ms 24240 KB Output is correct
57 Correct 122 ms 12296 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 63 ms 5880 KB Output is correct
62 Correct 268 ms 22660 KB Output is correct
63 Correct 253 ms 22448 KB Output is correct
64 Correct 268 ms 24312 KB Output is correct
65 Correct 1 ms 384 KB Output is correct
66 Correct 65 ms 5880 KB Output is correct
67 Correct 265 ms 22652 KB Output is correct
68 Correct 270 ms 22264 KB Output is correct
69 Correct 295 ms 25236 KB Output is correct
70 Correct 0 ms 384 KB Output is correct
71 Correct 0 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 70 ms 5884 KB Output is correct
74 Correct 261 ms 22840 KB Output is correct
75 Correct 266 ms 22660 KB Output is correct
76 Correct 262 ms 22412 KB Output is correct
77 Correct 271 ms 22220 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 1 ms 384 KB Output is correct
80 Correct 6 ms 1036 KB Output is correct
81 Correct 139 ms 16760 KB Output is correct
82 Correct 0 ms 384 KB Output is correct
83 Correct 1 ms 384 KB Output is correct
84 Correct 1 ms 384 KB Output is correct
85 Correct 65 ms 6008 KB Output is correct
86 Correct 126 ms 12792 KB Output is correct
87 Correct 124 ms 12792 KB Output is correct
88 Correct 119 ms 12408 KB Output is correct
89 Correct 119 ms 12280 KB Output is correct
90 Correct 1 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 6 ms 1024 KB Output is correct
93 Correct 134 ms 14988 KB Output is correct
94 Correct 1 ms 384 KB Output is correct
95 Correct 1 ms 384 KB Output is correct
96 Correct 1 ms 384 KB Output is correct
97 Correct 32 ms 3448 KB Output is correct
98 Correct 119 ms 12792 KB Output is correct
99 Correct 117 ms 12804 KB Output is correct
100 Correct 121 ms 12408 KB Output is correct
101 Correct 147 ms 12280 KB Output is correct
102 Correct 121 ms 12280 KB Output is correct
103 Correct 126 ms 15096 KB Output is correct
104 Correct 131 ms 14968 KB Output is correct
105 Correct 117 ms 12280 KB Output is correct