답안 #705080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705080 2023-03-03T11:16:58 Z Nursik Izlet (COI19_izlet) C++14
43 / 100
570 ms 84212 KB
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <cstdint>
#include <cassert>
#include <functional>
#include <complex>
#include <random>

using namespace std;
 
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
 
const ll maxn = 3e3 + 10, maxm = 20 + 1;
const ll mod = 1e9 + 7, cmod = 998244353, inf = 1e9, blcok = 400, p2 = 31;
const ld eps = 1e-9;

int s, n, a[maxn][maxn], p[maxn], sz[maxn], dep[maxn], color[maxn], used[maxn], par[maxn];
vector<int> comps[maxn];
vector<pair<int, int>> z[maxn];
vector<int> g[maxn];
void dfs(int v = 1, int p = 0, int cur = 1){
    if (p){
        ++cur;
        if (a[1][v] == cur){
            color[v] = v;
        }
        else{
            --cur;
            vector<int> vec;
            int is = v, prv = 1;
            while (is != 1){
                is = par[is];
                if (used[color[is]] == 0){
                    int x = a[is][v];
                    if (x == prv){
                        color[v] = color[is];
                        break;
                    }
                    ++prv;
                }
                used[color[is]] = 1;
            }
            for (int j = 1; j <= n; ++j){
                used[j] = 0;
            }
        }
    }
    for (auto to : g[v]){
        if (to != p){
            par[to] = v;
            dfs(to, v, cur);
        }
    }
}
int get(int v){
    if (v == p[v])
        return v;
    return p[v] = get(p[v]);
}
void unite(int a, int b){
    a = get(a), b = get(b);
    if (a == b)
        return;
    if (sz[a] > sz[b])
        swap(a, b);
    p[a] = b;
    sz[b] += sz[a];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); 
    cin >> s;
    cin >> n;
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= n; ++j){
            cin >> a[i][j];
        }
    }
    if (s == 1){
    for (int i = 1; i <= n; ++i){
        p[i] = i, sz[i] = 1;
        dep[i] = i;
    }
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= n; ++j){
            if (a[i][j] == 1){
                unite(i, j);
            }
        }
    }
    int root = get(1);
    for (int i = 1; i <= n; ++i){
        if (get(i) == root){
            color[i] = 1;
        }
        else{
            color[i] = 2;
        }
        comps[get(i)].pb(i);
        cout << color[i] << " ";
    }
    cout << '\n';
    vector<pair<int, int>> ans;
    for (int i = 1; i <= n; ++i){
        int sz = (int)comps[i].size() - 1;
        for (int j = 0; j < sz; ++j){
            ans.pb(mp(comps[i][j], comps[i][j + 1]));
        }
        if ((int)comps[i].size() > 0 && i != root){
            ans.pb(mp(comps[i][0], root));
        }
    }
    for (auto it : ans){
        cout << it.f << " " << it.s << '\n';
    }
    }
    else if (s == 2){
        int cur = 0;
        for (int i = 1; i <= n; ++i){
            ++cur;
            if (a[1][i] == cur){
                color[i] = i;
            }
            else{
                --cur;
                int prv = 1;
                for (int j = i - 1; j >= 1; --j){
                    if (used[color[j]] == 0){
                        int x = a[j][i];
                        if (x == prv){
                            color[i] = color[j];
                            break;
                        }
                        ++prv;
                    }
                    used[color[j]] = 1;
                }
                for (int j = 1; j <= i; ++j){
                    used[color[j]] = 0;
                }
            }
            cout << color[i] << " ";
        }
        cout << '\n';
        for (int i = 1; i < n; ++i){
            cout << i << " " << i + 1 << '\n';
        }
    }
    else{
        vector<pair<int, int>> ans;
        for (int i = 1; i <= n; ++i){
            for (int j = i + 1; j <= n; ++j){
                z[a[i][j]].pb(mp(i, j));
            }
        }
        for (int i = 1; i <= n; ++i){
            p[i] = i, sz[i] = 1;
            dep[i] = i;
        }
        for (int i = 1; i <= n; ++i){
            for (auto it : z[i]){
                if (get(it.f) != get(it.s)){
                    int ver = dep[it.f];
                    unite(ver, it.s);
                    g[ver].pb(it.s);
                    g[it.s].pb(ver);
                    ans.pb(mp(ver, it.s));
                    ver = get(ver);
                    dep[ver] = it.s;
                }
            }
        }
        color[1] = 1;
        dfs();
        for (int i = 1; i <= n; ++i){
            cout << color[i] << " ";
        }
        cout << '\n';
        for (auto it : ans){
            cout << it.f << " " << it.s << '\n';
        }
    }
}
/*
*/



# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 406 ms 36168 KB Output is correct
3 Correct 429 ms 36028 KB Output is correct
4 Correct 389 ms 35980 KB Output is correct
5 Correct 385 ms 36072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 407 ms 35820 KB Output is correct
2 Correct 390 ms 35972 KB Output is correct
3 Correct 503 ms 36004 KB Output is correct
4 Correct 570 ms 35860 KB Output is correct
5 Correct 428 ms 35924 KB Output is correct
6 Correct 468 ms 35892 KB Output is correct
7 Correct 311 ms 30328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 406 ms 36168 KB Output is correct
3 Correct 429 ms 36028 KB Output is correct
4 Correct 389 ms 35980 KB Output is correct
5 Correct 385 ms 36072 KB Output is correct
6 Correct 407 ms 35820 KB Output is correct
7 Correct 390 ms 35972 KB Output is correct
8 Correct 503 ms 36004 KB Output is correct
9 Correct 570 ms 35860 KB Output is correct
10 Correct 428 ms 35924 KB Output is correct
11 Correct 468 ms 35892 KB Output is correct
12 Correct 311 ms 30328 KB Output is correct
13 Incorrect 490 ms 84212 KB Integer 0 violates the range [1, 3000]
14 Halted 0 ms 0 KB -