답안 #877205

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
877205 2023-11-23T03:40:02 Z SyruLoveNewTechnology Rigged Roads (NOI19_riggedroads) C++14
100 / 100
206 ms 39740 KB
//SyruLoveNewTechnology
#include<bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int rand(const int &l, const int &r) {
    return rng() % (r - l + 1) + l;
}

void fre() {
    if(fopen("ZONING.inp", "r")) {
        freopen("ZONING.inp", "r", stdin);
        freopen("ZONING.out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

const int MAX = 300005;

int N, M;
int a[MAX], b[MAX];
int r1[MAX];
int w[MAX];
int par[MAX];
int p[15];
bool check[MAX];
int parent[MAX];
vector<int> adj[MAX];
int nex[MAX], h[MAX], t;

void read() {
    cin >> N >> M;
    for(int i = 1; i <= M; ++i)
        cin >> a[i] >> b[i];
    for(int i = 1; i < N; ++i)
        cin >> r1[i];
}

int root(int u) {
    if(par[u] < 0)
        return u;
    par[u] = root(par[u]);
    return par[u];
}

bool join(int u, int v) {
    u = root(u);
    v = root(v);
    if(u == v)
        return false;
    if(par[u] > par[v])
        swap(par[u], par[v]);
    par[u] += par[v];
    par[v] = u;
    return true;
}

bool check1() {
    fill(par + 1, par + N + 1, -1);
    for(int i = 1; i <= M; ++i)
        p[w[i]] = i;
    for(int i = 1; i <= M; ++i) {
        bool c = join(a[p[i]], b[p[i]]);
        if(check[p[i]] != c)
            return false;
    }
    return true;
}

void sub1() {
    for(int i = 1; i <= M; ++i)
        w[i] = i;
    for(int i = 1; i < N; ++i)
        check[r1[i]] = true;
    if(check1()) {
        for(int i = 1; i <= M; ++i)
            cout << w[i] << ' ';
        cout << '\n';
        return;
    }
    while(next_permutation(w + 1, w + M + 1)) {
        if(check1()) {
            for(int i = 1; i <= M; ++i)
                cout << w[i] << ' ';
            cout << '\n';
            return;
        }
    }
}

void dfs(int u, int p) {
    h[u] = h[p] + 1;
    for(int i : adj[u]) {
        int v = u ^ a[i] ^ b[i];
        if(v != p) {
            parent[v] = i;
            dfs(v, u);
        }
    }
}

void add(int i) {
    if(parent[b[i]] == i)
        swap(a[i], b[i]);
    nex[a[i]] = b[i];
}

int ROOT(int u) {
    if(nex[u] == u)
        return u;
    nex[u] = ROOT(nex[u]);
    return nex[u];
}

void solve() {
    for(int i = 1; i < N; ++i) {
        check[r1[i]] = true;
        adj[a[r1[i]]].emplace_back(r1[i]);
        adj[b[r1[i]]].emplace_back(r1[i]);
    }
    dfs(1, 0);
    for(int i = 1; i <= N; ++i)
        nex[i] = i;
    for(int i = 1; i <= M; ++i)
        if(w[i] == 0) {
            if(check[i]) {
                ++t;
                w[i] = t;
                add(i);
            }
            else {
                int ra = ROOT(a[i]);
                int rb = ROOT(b[i]);
                if(ra == rb) {
                    ++t;
                    w[i] = t;
                }
                else {
                    vector<int> q;
                    a[i] = nex[a[i]];
                    b[i] = nex[b[i]];
                    while(a[i] != b[i]) {
                        if(h[a[i]] < h[b[i]])
                            swap(a[i], b[i]);
                        q.emplace_back(parent[a[i]]);
                        add(parent[a[i]]);
                        a[i] = ROOT(a[i]);
                    }
                    sort(q.begin(), q.end());
                    for(int i : q) {
                        ++t;
                        w[i] = t;
                    }
                    ++t;
                    w[i] = t;
                }
            }
        }
    for(int i = 1; i <= M; ++i)
        cout << w[i] << ' ';
    cout << '\n';
}

main() {
    fre();
    read();
    if(N <= 9 && M <= 9)
        sub1();
    else
        solve();
}

Compilation message

riggedroads.cpp:167:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  167 | main() {
      | ^~~~
riggedroads.cpp: In function 'void fre()':
riggedroads.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |         freopen("ZONING.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen("ZONING.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16836 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 20944 KB Output is correct
2 Correct 53 ms 22548 KB Output is correct
3 Correct 53 ms 19284 KB Output is correct
4 Correct 79 ms 28352 KB Output is correct
5 Correct 88 ms 29076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 27648 KB Output is correct
2 Correct 40 ms 21072 KB Output is correct
3 Correct 18 ms 19032 KB Output is correct
4 Correct 41 ms 24880 KB Output is correct
5 Correct 17 ms 20316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 34972 KB Output is correct
2 Correct 206 ms 39740 KB Output is correct
3 Correct 32 ms 23288 KB Output is correct
4 Correct 48 ms 26500 KB Output is correct
5 Correct 163 ms 38492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 30912 KB Output is correct
2 Correct 51 ms 26708 KB Output is correct
3 Correct 181 ms 35156 KB Output is correct
4 Correct 154 ms 33368 KB Output is correct
5 Correct 14 ms 18008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 3 ms 14680 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16836 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 30 ms 20944 KB Output is correct
10 Correct 53 ms 22548 KB Output is correct
11 Correct 53 ms 19284 KB Output is correct
12 Correct 79 ms 28352 KB Output is correct
13 Correct 88 ms 29076 KB Output is correct
14 Correct 55 ms 27648 KB Output is correct
15 Correct 40 ms 21072 KB Output is correct
16 Correct 18 ms 19032 KB Output is correct
17 Correct 41 ms 24880 KB Output is correct
18 Correct 17 ms 20316 KB Output is correct
19 Correct 125 ms 34972 KB Output is correct
20 Correct 206 ms 39740 KB Output is correct
21 Correct 32 ms 23288 KB Output is correct
22 Correct 48 ms 26500 KB Output is correct
23 Correct 163 ms 38492 KB Output is correct
24 Correct 90 ms 30912 KB Output is correct
25 Correct 51 ms 26708 KB Output is correct
26 Correct 181 ms 35156 KB Output is correct
27 Correct 154 ms 33368 KB Output is correct
28 Correct 14 ms 18008 KB Output is correct
29 Correct 159 ms 33308 KB Output is correct
30 Correct 171 ms 32028 KB Output is correct
31 Correct 156 ms 29492 KB Output is correct
32 Correct 52 ms 20048 KB Output is correct
33 Correct 156 ms 30220 KB Output is correct