QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#833455#852. Jellyfishucup-team5071#WA 180ms3868kbC++202.7kb2024-12-26 19:54:132024-12-26 19:54:14

Judging History

This is the latest submission verdict.

  • [2024-12-26 19:54:14]
  • Judged
  • Verdict: WA
  • Time: 180ms
  • Memory: 3868kb
  • [2024-12-26 19:54:13]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> p)
{
    out << "(" << p.first << "," << p.second << ")";
    return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> v)
{
    out << "[";
    if (!v.empty())
        cout << v[0];
    for (int i = 1; i < (int)v.size(); i++)
        out << "," << v[i];
    out << "]";
    return out;
}
int solve()
{
    int n;
    cin >> n;
    vector<vector<int>> ve(n + 1);
    vector<int> cnt(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        cin >> x >> y;
        ve[x].push_back(y);
        ve[y].push_back(x);
        cnt[x]++, cnt[y]++;
    }
    queue<int> qu;
    vector<int> vis(n + 1);
    for (int i = 1; i <= n; i++)
        if (cnt[i] == 1)
        {
            qu.push(i);
        }
    while (!qu.empty())
    {
        int x = qu.front();
        qu.pop();
        vis[x] = 1;
        for (auto it : ve[x])
            if ((--cnt[it] == 1) && vis[it] == 0)
                qu.push(it);
    }
    int ans = 3;
    vector<int> circle;
    vector<int> in_circle(n + 1);
    for (int i = 1; i <= n; i++)
        if (!vis[i])
        {
            function<void(int)> dfs = [&](int x)
            {
                if (vis[x])
                    return;
                in_circle[x] = 1;
                circle.push_back(x);
                vis[x] = 1;
                for (auto it : ve[x])
                    if (!vis[it])
                        dfs(it);
            };
            dfs(i);
        }
    int single = 0, sum = 0;
    for (auto it : circle)
    {
        if ((int)ve[it].size() == 2)
        {
            single++;
        }
        else
        {

            for (auto t : ve[it])
                if (!in_circle[t])
                {
                    function<void(int, int)> dfs = [&](int x, int h)
                    {
                        if ((int)ve[x].size() == 1 && ve[x].front() == h)
                        {
                            sum++;
                        }
                        for (auto it : ve[x])
                            if (it != h)
                            {
                                dfs(it, x);
                            }
                    };
                    dfs(t, it);
                }
        }
    }
    // cout << "circle=" << circle << endl;
    // cout << single << " " << sum << endl;
    return max(ans, min(single, 2) + sum);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--)
        cout << solve() << "\n";
}

詳細信息

Test #1:

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

input:

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

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: -100
Wrong Answer
time: 180ms
memory: 3868kb

input:

85665
6
3 2
4 1
4 6
2 1
2 6
5 1
7
6 2
6 3
5 1
1 2
2 4
4 7
3 7
7
6 1
6 7
1 4
1 3
7 5
5 3
4 2
7
6 2
7 4
7 5
3 1
3 4
2 5
1 4
7
7 2
2 6
5 4
5 6
5 1
3 1
4 6
7
3 5
3 1
3 7
3 2
5 1
5 4
4 6
7
4 5
4 1
3 6
3 7
6 7
6 1
2 1
7
5 3
7 3
1 4
6 2
6 3
2 3
4 3
7
2 3
2 6
2 4
7 5
3 5
5 1
1 4
7
3 4
3 7
5 6
2 7
4 6
6 7
6 ...

output:

4
3
3
3
3
4
4
5
4
5
4
4
3
4
4
3
4
4
4
4
4
5
3
4
3
4
3
9
4
4
3
4
8
3
98
5
4
3
6
4
4
4
4
3
4
4
4
4
5
3
5
4
3
4
95
4
4
4
5
4
3
4
3
5
4
3
4
3
3
4
4
4
4
4
3
4
4
4
3
3
3
4
4
4
4
4
4
5
4
4
3
3
5
5
4
5
4
3
4
4
3
3
3
5
4
4
4
6
4
5
5
5
4
3
5
4
4
3
4
10
4
3
3
4
4
4
5
4
4
3
5
3
4
4
3
3
3
4
5
98
5
105
4
4
4
3
4
...

result:

wrong answer 84th numbers differ - expected: '3', found: '4'