QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114627#852. Jellyfish202032110146AC ✓182ms23664kbC++172.9kb2023-06-22 17:51:402023-06-22 17:51:41

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 17:51:41]
  • Judged
  • Verdict: AC
  • Time: 182ms
  • Memory: 23664kb
  • [2023-06-22 17:51:40]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#pragma GCC optimize(3)
#define debug(x...)             \
    do {                      \
        cout << #x << " -> ";\
        err(x);               \
    } while (0)

void err() {
    cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
    cout << arg << ' ';
    err(args...);
}

const ll INF = 0x3f3f3f3f3f3f3f3f;//2147483647;
const ll MOD[2] = {1000000007, 998244353};
const ll base[2] = {131, 13331};
const double pi = acos(-1.0);

const int N = 25050, M = 5050 << 1;
const ll mod = MOD[0];

int n;

void dfs(int x, int pre, vector<vector<int>> &to, vector<int> &vis, stack<int> &stk, vector<int> &loop) {
    if (!loop.empty())return;
    for (auto v: to[x]) {
        if (v == pre)continue;
        if (!loop.empty())return;
        if (vis[v]) {
            while (!stk.empty() && stk.top() != v) {
                loop.push_back(stk.top());
                stk.pop();
            }
            loop.push_back(v);
            return;
        }
        stk.push(v);
        vis[v] = 1;
        dfs(v, x, to, vis, stk, loop);
        if (!loop.empty())return;
        stk.pop();
        vis[v] = 0;
    }
}

int DFS(int x, int pre, vector<vector<int>> &to, vector<int> &vis) {
    if (to[x].size() == 1)return 1;
    int res = 0;
    for (auto v: to[x]) {
        if (v == pre)continue;
        if (vis[v])continue;
        res += DFS(v, x, to, vis);
    }
    return res;
}

void solve() {
    cin >> n;
    vector<vector<int>> G(n + 1, vector<int>());
    for (int i = 1, u, v; i <= n; i++) {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<int> vis(n + 1, 0);
    stack<int> stk;
    stk.push(1);
    vis[1] = 1;
    vector<int> loop;
    dfs(1, 0, G, vis, stk, loop);
    vector<int> in_loop(n + 1, 0);
    for (auto i: loop) {
        in_loop[i] = 1;
    }
    vector<int> cal(n + 1, 0);
    int ans = 3;
    for (auto i: loop) {
        cal[i] = DFS(i, 0, G, in_loop);
    }
    int res = 0;
    int neighbor = 0;
    int cnt = 0;
    for (int i = 0; i < (int) loop.size(); i++) {
        if (cal[loop[i]]) {
            res += cal[loop[i]];
            cnt++;
        } else {
            int nxt = loop[(i + 1) % (int) loop.size()];
            if (!cal[nxt])neighbor = 1;
        }
    }
    if (cnt == loop.size()) {
        ans = res;
    } else if (neighbor) {
        ans = max(ans, res + 2);
    } else {
        ans = max(ans, res + 1);
    }
    cout << ans << "\n";
}


signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _ = 1;
    cin >> _;
    for (int i = 1; i <= _; i++) {
        solve();
    }
    return 0;
}
/*
1
7
7 6
2 3
2 5
2 6
5 6
5 4
1 6
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 175ms
memory: 3780kb

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
3
4
4
4
4
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
3
5
4
4
3
5
3
4
4
3
3
3
4
5
98
5
105
4
4
4
3
4
...

result:

ok 85665 numbers

Test #3:

score: 0
Accepted
time: 182ms
memory: 23664kb

input:

30
2229
1066 248
248 881
881 2080
2080 1615
1615 1647
1647 1774
1774 196
196 434
434 390
390 129
129 563
563 63
63 1457
1457 1015
1015 2200
2200 1187
1187 1763
1763 1121
1121 2122
2122 1783
1783 1756
1756 2031
2031 2153
2153 605
605 1778
1778 1287
1287 2062
2062 817
817 194
194 474
474 414
414 1736
...

output:

1092
881
722
1412
37556
638
438
509
273
29198
740
27535
46011
865
444
30031
49564
794
489
469
624
956
1180
17384
50000
715
1291
49920
1465
3

result:

ok 30 numbers