답안 #611438

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
611438 2022-07-29T04:57:55 Z 박상훈(#8496) Izlet (COI19_izlet) C++17
100 / 100
770 ms 119320 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
vector<int> adj[3030];
int a[3030][3030], col[3030], cnt, on[3030], oncnt;
bool used[3030];

int dfs(int s, int pa, int o){
    if (!on[col[s]]) oncnt++;
    on[col[s]]++;

    if (oncnt==a[o][s]) return s;

    for (auto &v:adj[s]) if (v!=pa){
        int ret = dfs(v, s, o);
        if (ret!=-1) return ret;
    }

    --on[col[s]];
    if (!on[col[s]]) --oncnt;
    return -1;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int subtask;
    cin >> subtask;

    int n;
    cin >> n;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++) cin >> a[i][j];
    }

    vector<pair<int, int>> q[3];
    used[1] = 1, col[1] = ++cnt;
    for (int j=2;j<=n;j++) if (a[1][j]<=2){
        q[a[1][j]].emplace_back(j, 1);
    }

    while(!q[1].empty() || !q[2].empty()){
        if (!q[1].empty()){
            auto [s, pa] = q[1].back(); q[1].pop_back();
            if (used[s]) continue;
            adj[pa].push_back(s);
            adj[s].push_back(pa);
            used[s] = 1;
            col[s] = col[pa];

            for (int i=1;i<=n;i++) if (!used[i] && a[s][i]<=2){
                q[a[s][i]].emplace_back(i, s);
            }
        }
        else{
            auto [s, pa] = q[2].back(); q[2].pop_back();
            if (used[s]) continue;

            fill(on+1, on+n+1, 0); oncnt = 0;
            int c = dfs(pa, -1, s);
            if (c==-1) col[s] = ++cnt;
            else col[s] = col[c];

            adj[pa].push_back(s);
            adj[s].push_back(pa);
            used[s] = 1;

            for (int i=1;i<=n;i++) if (!used[i] && a[s][i]<=2){
                q[a[s][i]].emplace_back(i, s);
            }
        }
    }

    for (int i=1;i<=n;i++) cout << col[i] << ' ';
    cout << '\n';
    for (int i=1;i<=n;i++){
        for (auto &j:adj[i]) if (i<j) cout << i << ' ' << j << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 496 ms 102152 KB Output is correct
3 Correct 511 ms 102112 KB Output is correct
4 Correct 501 ms 103820 KB Output is correct
5 Correct 503 ms 105448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 426 ms 36496 KB Output is correct
2 Correct 575 ms 119320 KB Output is correct
3 Correct 632 ms 74056 KB Output is correct
4 Correct 770 ms 74816 KB Output is correct
5 Correct 533 ms 60804 KB Output is correct
6 Correct 563 ms 61412 KB Output is correct
7 Correct 364 ms 50320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 496 ms 102152 KB Output is correct
3 Correct 511 ms 102112 KB Output is correct
4 Correct 501 ms 103820 KB Output is correct
5 Correct 503 ms 105448 KB Output is correct
6 Correct 426 ms 36496 KB Output is correct
7 Correct 575 ms 119320 KB Output is correct
8 Correct 632 ms 74056 KB Output is correct
9 Correct 770 ms 74816 KB Output is correct
10 Correct 533 ms 60804 KB Output is correct
11 Correct 563 ms 61412 KB Output is correct
12 Correct 364 ms 50320 KB Output is correct
13 Correct 504 ms 54540 KB Output is correct
14 Correct 603 ms 61624 KB Output is correct
15 Correct 467 ms 64356 KB Output is correct
16 Correct 571 ms 63284 KB Output is correct
17 Correct 638 ms 64360 KB Output is correct
18 Correct 483 ms 53724 KB Output is correct
19 Correct 460 ms 76228 KB Output is correct
20 Correct 483 ms 64932 KB Output is correct
21 Correct 481 ms 65880 KB Output is correct
22 Correct 516 ms 55096 KB Output is correct
23 Correct 560 ms 55116 KB Output is correct
24 Correct 520 ms 60828 KB Output is correct
25 Correct 463 ms 56004 KB Output is correct