QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661736#7898. I Just Want... One More...AndevikingWA 8ms3640kbC++202.9kb2024-10-20 17:51:062024-10-20 17:51:07

Judging History

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

  • [2024-10-20 17:51:07]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:3640kb
  • [2024-10-20 17:51:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define range(x) (x).begin(), (x).end()
const int dir[][2] = {1, 0, -1, 0, 0, 1, 0, -1};

void solve()
{
    int n, m;
    cin >> n >> m;

    vector<vector<int>> e(2 * n + 5);
    for (int i = 1; i <= m; ++i) {
        int u, v;
        cin >> u >> v;
        e[u].push_back(n + v);
        e[n + v].push_back(u);
    }

    vector<int> vis(2 * n + 5);
    ll sum[2] = {0};

    for (int i = 1; i <= 2 * n; ++i) {
        if (vis[i])
            continue;
        queue<int> q;
        q.push(i);
        vis[i] = 1;
        vector<int> use;
        int cnt[2] = {0, 0};
        cnt[i > n] = 1;
        while (!q.empty()) {
            auto x = q.front();
            q.pop();
            use.push_back(x);
            for (const auto &y : e[x]) {
                if (vis[y])
                    continue;
                vis[y] = 1;
                cnt[y > n]++;
                q.push(y);
            }
        }

        if (cnt[0] > cnt[1]) {
            map<int, set<int>> deg;
            for (const auto &c : use)
                if (c > n) {
                    for (const auto &y : e[c])
                        deg[c].insert(y);
                    if (deg[c].size() == 1)
                        q.push(c);
                }
            int sz = cnt[0];

            while (!q.empty()) {
                auto x = q.front();
                q.pop();
                assert(deg[x].size());
                int now = *deg[x].begin();
                sz--;
                for (const auto &y : e[now]) {
                    assert(deg[y].size());
                    deg[y].erase(now);
                    if (deg[y].size() == 1)
                        q.push(y);
                }
            }
            assert(sz > 0);
            sum[0] += sz;
        }
        else if (cnt[0] < cnt[1]) {
            map<int, set<int>> deg;
            for (const auto &c : use)
                if (c <= n) {
                    for (const auto &y : e[c])
                        deg[c].insert(y);
                    if (deg[c].size() == 1)
                        q.push(c);
                }

            int sz = cnt[1];
            while (!q.empty()) {
                auto x = q.front();
                q.pop();
                assert(deg[x].size());
                int now = *deg[x].begin();
                sz--;
                for (const auto &y : e[now]) {
                    assert(deg[y].size());

                    deg[y].erase(now);
                    if (deg[y].size() == 1)
                        q.push(y);
                }
            }
            assert(sz > 0);
            sum[1] += sz;
        }
    }

    cout << sum[0] * sum[1] << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

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

output:

6
0
4

result:

ok 3 number(s): "6 0 4"

Test #2:

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

input:

10000
5 4
2 2
3 1
5 3
1 1
1 1
1 1
1 1
1 1
2 2
2 2
1 2
1 1
1 1
1 1
1 1
1 1
1 1
2 3
2 1
1 2
1 1
5 5
1 4
2 2
3 1
1 3
1 2
2 4
2 2
2 1
1 2
1 1
5 1
5 3
3 1
2 2
1 1
1 1
3 2
3 1
2 1
5 2
1 2
2 3
5 3
1 5
4 2
1 2
4 1
1 1
2 3
1 1
2 2
2 1
4 1
1 4
3 1
1 1
1 1
1 1
2 1
2 2
3 3
1 3
2 3
2 2
3 3
1 3
3 3
1 2
3 3
2 2
1 ...

output:

6
0
0
2
0
0
0
0
6
0
16
4
0
6
9
9
9
0
9
4
0
1
1
1
0
4
16
12
3
2
16
0
2
2
20
1
0
0
0
0
16
4
4
16
4
9
0
9
0
2
3
0
9
4
9
16
20
0
0
1
12
0
1
2
0
0
1
0
0
2
2
4
0
12
1
0
0
2
1
2
2
3
0
4
1
6
0
0
0
0
9
16
2
0
1
2
0
12
2
4
0
12
1
1
9
4
6
9
9
12
3
16
15
16
9
4
9
0
1
16
9
9
1
9
16
9
12
4
9
2
0
4
0
6
0
3
0
0
0
0...

result:

wrong answer 305th numbers differ - expected: '4', found: '0'