QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#140865#5160. Kebab PizzabeshoyhanyWA 1ms3576kbC++143.7kb2023-08-16 21:47:342023-08-16 21:47:35

Judging History

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

  • [2023-08-16 21:47:35]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3576kb
  • [2023-08-16 21:47:34]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll

using namespace std;

void Drakon() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef Clion
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}

unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25;

ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
    return (a * b) / __gcd(a, b);
}

ll mul(const ll &a, const ll &b) {
    return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
}

ll add(const ll &a, const ll &b) {
    return (a + b + 2 * MOD) % MOD;
}

ll pw(ll x, ll y) {
    ll ret = 1;
    while (y > 0) {
        if (y % 2 == 0) {
            x = mul(x, x);
            y = y / 2;
        } else {
            ret = mul(ret, x);
            y = y - 1;
        }
    }
    return ret;
}

void solve() {
    int n, k;
    cin >> n >> k;
    map<int, int> freq;
    map<int, multiset<int>> mp;
    set<int> alive;
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        alive.insert(a);
        alive.insert(b);
        if (a == b) {
            continue;
        }
        freq[a]++;
        freq[b]++;
        mp[a].insert(b);
        mp[b].insert(a);
    }

    int f = -1, s = -1, cnt = 0;

    while (!alive.empty()) {
        if (~f) {
            bool ok = (f == s);
            alive.erase(f);
            multiset<int> ms = mp[f];
            vector<int> vec;
            for (auto x: ms) {
                if (!alive.count(x))continue;
                vec.push_back(x);
            }
            map<int, int> cur;
            vector<int> rem;
            for (auto x: vec) {
                if (rem.empty() || rem.back() != x)rem.pp(x);
                cur[x]++;
                if (cur[x] == freq[x])rem.pop_back(), alive.erase(x);
            }

            if (rem.size() > 1) {
                cout << "impossible";
                return;
            } else if (rem.size() == 1) {
                f = rem.back();
            } else {
                f = -1;
                swap(f, s);
            }
            if (ok)break;
        }
        else {
            int z = *alive.begin();
            alive.erase(z);
            multiset<int> ms = mp[z];
            vector<int> vec;
            for (auto x: ms) {
                if (!alive.count(x))continue;
                vec.push_back(x);
            }
            map<int, int> cur;
            vector<int> rem;
            for (auto x: vec) {
                if (rem.empty() || rem.back() != x)rem.pp(x);
                cur[x]++;
                if (cur[x] == freq[x])rem.pop_back(), alive.erase(x);
            }

            if (rem.size() > 2) {
                cout << "impossible";
                return;
            } else if (rem.size() == 2) {
                if(cnt){
                    cout << "impossible";
                    return;
                }
                f = rem[0];
                s = rem[1];
            } else if (rem.size() == 1) {
                f = rem[0];
            }
            cnt ++;
        }
    }

    if (alive.size()) {
        cout << "impossible";
        return;
    }
    cout << "possible";
}

signed main() {
    Drakon();
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3576kb

input:

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

output:

possible

result:

ok single line: 'possible'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3504kb

input:

5 5
1 3
1 5
2 3
2 5
3 4

output:

possible

result:

ok single line: 'possible'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

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

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

8 4
1 1
1 2
2 1
2 2
3 3
3 4
4 3
4 4

output:

possible

result:

ok single line: 'possible'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3476kb

input:

4 4
1 2
2 1
3 4
4 3

output:

possible

result:

ok single line: 'possible'

Test #6:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

5 4
1 1
1 4
2 2
2 4
3 4

output:

possible

result:

ok single line: 'possible'

Test #7:

score: -100
Wrong Answer
time: 1ms
memory: 3544kb

input:

6 4
1 1
1 4
2 2
2 4
3 3
3 4

output:

possible

result:

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