80pts WA#8 RE#10 求调
查看原帖
80pts WA#8 RE#10 求调
1036180
ChampionCyan楼主2025/8/29 11:10
#include <bits/stdc++.h>
#define ERROR puts("Impossible"), exit(0)
using namespace std;
const int MAXN = 250005;
vector<int> g[MAXN];
int n, in[MAXN], out[MAXN];
bool f1, f2;
map<string, int> mp;
bitset<MAXN> vis;

void check() {
    queue<int> q;
    int st = 1;
    for (int i = 1; i <= n; i++)
        if (out[i] - in[i] == 1) {
            st = i;
            break;
        }
    q.push(st);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int v : g[u])
            q.push(v);
    }
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            ERROR;
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    string s, t;
    while (cin >> s >> t) {
        int u = (mp[s] ? mp[s] : mp[s] = ++n), v = (mp[t] ? mp[t] : mp[t] = ++n);
        g[u].emplace_back(v);
        in[v]++, out[u]++;
    }
    check();
    for (int i = 1; i <= n; i++) {
        if (abs(in[i] - out[i]) > 1)
            ERROR;
        if (in[i] - out[i] == 1) {
            if (f1)
                ERROR;
            f1 = true;
        }
        if (out[i] - in[i] == 1) {
            if (f2)
                ERROR;
            f2 = true;
        }
    }
    f1 == f2 ? puts("Possible") : ERROR;
    return 0;
}
2025/8/29 11:10
加载中...