QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432591#8686. Zoo ManagementORzyzROWA 8ms46840kbC++143.8kb2024-06-07 12:53:432024-06-07 12:53:44

Judging History

你现在查看的是最新测评结果

  • [2024-06-07 12:53:44]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:46840kb
  • [2024-06-07 12:53:43]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pr pair<int, int>
#define pb push_back
#define mid (l + r) / 2
#define ls num << 1
#define rs num << 1 | 1

inline int read() {
    int x = 0, m = 1;
    char ch = getchar();

    while (!isdigit(ch)) {
        if (ch == '-') m = -1;
        ch = getchar();
    }

    while (isdigit(ch)) {
        x = x * 10 + ch - 48;
        ch = getchar();
    }

    return x * m;
}

inline void write(int x) {
    if (x < 0) {
        putchar('-');
        write(-x);
        return;
    }

    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

const int N = 4e5 + 5;

int a[N], b[N], c[N << 1], ca[N], cb[N];
int dfn[N], low[N], vis[N], id[N], P[N];

stack<int> st;
vector<pr> v[N];
vector<int> val[N], Val[N];

int num = 0, cnt = 0;

void Tarjan(int ro, int fa) {
    dfn[ro] = low[ro] = ++num;
    st.push(ro);
    for (auto u : v[ro]) {
        int to = u.first, id = u.second;
        if (id == fa) continue;
        if (!dfn[to]) {
            Tarjan(to, id);
            low[ro] = min(low[ro], low[to]);
        }
        else low[ro] = min(low[ro], dfn[to]);
    }
    if (dfn[ro] == low[ro]) {
        int now;
        cnt++;
        vector<int> dot, col1, col2;
        dot.clear();
        col1.clear();
        col2.clear();
        do {
            now = st.top();
            st.pop();
            id[now] = cnt;
            dot.pb(now);
        } while (now != ro);
        int res = 0;
        for (auto u : dot) {
            col1.pb(a[u]);
            col2.pb(b[u]);
            for (auto x : v[u]) {
                if (id[x.first] == id[u]) res++;
            }
        }
        sort(col1.begin(), col1.end());
        sort(col2.begin(), col2.end());
        for (int i = 0; i < col1.size(); i++) {
            if (col1[i] != col2[i]) {
                puts("impossible");
                exit(0);
            }
        }
        res /= 2;
        if (res == dot.size()) {
            for (auto u : dot) {
                for (auto x : v[u]) {
                    if (id[x.first] == id[u]) {
                        val[a[u]].pb(a[x.first]);
                        Val[b[u]].pb(b[x.first]);
                    }
                }
            }
            for (auto u : col1) {
                sort(val[u].begin(), val[u].end());
                sort(Val[u].begin(), Val[u].end());
                if (val[u].size() != Val[u].size()) {
                    puts("impossible");
                    exit(0);
                }
                for (int i = 0; i < val[u].size(); i++) {
                    if (val[u][i] != Val[u][i]) {
                        puts("impossible");
                        exit(0);
                    }
                }
                val[u].clear();
                Val[u].clear();
            }
        }
    }
}

signed main() {
    int n = read(), m = read(), k = 0;

    for (int i = 1; i <= n; i++) {
        a[i] = read();
        b[i] = read();
        c[++k] = a[i];
        c[++k] = b[i];
    }

    sort(c + 1, c + k + 1);
    k = unique(c + 1, c + k + 1) - c - 1;

    if (k > n) {
        puts("impossible");
        return 0;
    }

    for (int i = 1; i <= n; i++) {
        a[i] = lower_bound(c + 1, c + k + 1, a[i]) - c;
        b[i] = lower_bound(c + 1, c + k + 1, b[i]) - c;
        ca[a[i]]++;
        cb[b[i]]++;
    }
    for (int i = 1; i <= k; i++) {
        if (ca[i] != cb[i]) {
            puts("impossible");
            return 0;
        }
    }

    for (int i = 1; i <= m; i++) {
        int x = read(), y = read();
        v[x].pb({y, i});
        v[y].pb({x, i});
    }

    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) Tarjan(i, 0);
    }

    puts("possible");
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 46840kb

input:

6 7
1 1
2 2
3 3
1 2
2 3
3 1
1 2
2 3
1 3
3 4
4 5
5 6
4 6

output:

possible

result:

ok single line: 'possible'

Test #2:

score: -100
Wrong Answer
time: 8ms
memory: 46580kb

input:

5 6
10 10
20 20
30 30
40 50
50 40
1 2
2 3
1 3
3 4
3 5
4 5

output:

possible

result:

wrong answer 1st lines differ - expected: 'impossible', found: 'possible'