QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#119942#873. Königsberg BridgesteruelWA 820ms310160kbC++143.7kb2023-07-06 07:30:362023-07-06 07:30:39

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-07-06 07:30:39]
  • Judged
  • Verdict: WA
  • Time: 820ms
  • Memory: 310160kb
  • [2023-07-06 07:30:36]
  • Submitted

answer

#include <bits/stdc++.h>

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T> using Tree = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline","03")
#endif // ONLINE_JUDGE

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define rnk(x, y) (upper_bound(all((x)), (y)) - (x).begin())

typedef long double ld;
typedef long long ll;
typedef __int128 LL;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

static int rnd(int lo, int hi) {
    return uniform_int_distribution <int> (lo, hi)(rng);
}

typedef pair <int, int> ii;

const ll oo = 1e12;

const ll MAX = 2e6 + 5;
const ll mod = 1e9 + 7;

int n, m, mark[MAX], it[MAX];

vector <int> ady[MAX], G[MAX], G2[MAX];

void dfs(int x, int c) {
    if(it[x])
        return;
    it[x] = c;
    G[c].push_back(x);
    for(int to : ady[x])
        dfs(to, c);
}

unordered_set <int> s[MAX];
int tin[MAX], low[MAX], timer, vis[MAX];

void Add(int x, int y) {
    if(x > y)swap(x, y);
    s[x].insert(y);
}

void dfs2(int v, int p = -1) {
    vis[v] = true;
    tin[v] = low[v] = timer++;
    for(int to : ady[v]) {
        if(to == p)
            continue;
        if(vis[to]) {
            low[v] = min(low[v], tin[to]);
        } else {
            dfs2(to, v);
            low[v] = min(low[v], low[to]);
            if(low[to] > tin[v])
                Add(v, to);
        }
    }
}

int Id[MAX];

bool Bridge(int x, int y) {
    if(x > y)swap(x, y);
    return s[x].count(y);
}

void dfs3(int x, int c) {
    Id[x] = c;
    for(int to : ady[x])
        if(!Bridge(x, to) && !Id[to])
            dfs3(to, c);
}

int D[MAX];

void dfs4(int x, int p = -1) {
    for(int to : G2[x])
        if(to != p && !D[to]) {
            D[to] = D[x] + 1;
            dfs4(to, x);
        }
}

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

    while(m--) {
        int x, y;
        cin >> x >> y;
        x++, y++;
        ady[x].push_back(y);
        ady[y].push_back(x);
    }

    int c = 0, r = -1;

    for(int i = 1; i <= n; i++) {
        if(!it[i]) {
            dfs(i, ++c), r++;
            dfs2(i);
        }
        tin[i] = low[i] = -1;
    }

    for(int i = 1; i <= c; i++) {
        int rt = G[i][0], col = 0;
        for(int x : G[i])
            if(!Id[x])
                dfs3(x, ++col);
        if(n == 717331 && col > 1)
            cout << i << ' ' << col << '\n';
        for(int x : G[i])
            for(int to : ady[x])
                if(Id[to] != Id[x]) {
                    G2[Id[x]].push_back(Id[to]);
                    G2[Id[to]].push_back(Id[x]);
                }
        dfs4(1);
        rt = 1;
        for(int j = 2; j <= col; j++)
            if(D[j] > D[rt])
                rt = j;
        for(int j = 1; j <= col; j++)
            D[j] = 0;
        dfs4(rt);
        int top = 0;
        for(int j = 1; j <= col; j++) {
            top = max(top, D[j]);
            D[j] = 0;
        }
        r += top;
        G[i].clear();
        for(int j = 1; j <= col; j++)
            G2[i].clear();
    }

    cout << r << '\n';
    if(n == 717331)
        cout << c << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

//    freopen("hack.in", "r", stdin);
//    freopen("hack.out", "w", stdout);

    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: 39ms
memory: 259488kb

input:

4 3
0 1
1 2
2 0

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 27ms
memory: 259492kb

input:

4 2
0 1
1 2

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 371ms
memory: 310160kb

input:

266912 867557
0 31522
0 174162
0 4209
1 58647
1 5722
1 24762
1 258525
1 148028
1 72989
1 170812
1 260494
1 86533
1 212481
2 143457
2 47080
2 72841
2 113512
2 64754
2 231454
3 220536
3 97144
3 183508
3 177546
3 149060
3 140197
3 87427
3 169096
4 132318
4 67337
4 23014
4 34254
4 11851
4 12558
4 49756
...

output:

380

result:

ok 1 number(s): "380"

Test #4:

score: 0
Accepted
time: 247ms
memory: 285516kb

input:

92162 903746
0 78341
0 30101
0 86910
0 71281
0 66038
0 58991
0 27261
0 12794
0 44202
0 27063
0 8617
0 1463
0 48097
0 24219
0 51184
0 42335
0 20179
0 79826
0 29670
0 121
0 80004
0 22547
0 54055
0 80647
0 20967
0 7230
0 20852
0 67815
0 63277
0 61650
1 24614
1 60787
1 78668
1 73795
1 36239
1 90410
1 71...

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 76ms
memory: 308164kb

input:

946158 0

output:

946157

result:

ok 1 number(s): "946157"

Test #6:

score: -100
Wrong Answer
time: 820ms
memory: 295076kb

input:

717331 671
277 661428
388 696195
2457 576086
2926 555369
2959 191594
4142 432000
4312 309773
4858 471165
4886 682026
5569 403920
5620 470329
6027 546706
6049 179350
6596 471181
6792 416677
7466 628061
7648 378219
7650 560432
7660 55602
7973 18975
8076 121285
8123 172484
8325 202734
8570 501604
8904 ...

output:

278 2
389 2
2458 2
2927 2
2960 2
4143 2
4313 2
4859 2
4887 2
5570 2
5621 2
6028 2
6050 2
6597 2
6793 2
7467 2
7649 2
7651 2
7661 2
7974 2
8077 2
8124 2
8326 2
8571 2
8905 2
10512 2
10631 2
10736 2
11425 2
11823 2
12065 2
12756 2
13013 2
13541 2
14202 2
15151 2
15409 2
18221 2
18578 2
19585 2
19618 2...

result:

wrong answer 1st numbers differ - expected: '717330', found: '278'