QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#119958#873. Königsberg BridgesteruelTL 419ms308284kbC++143.9kb2023-07-06 07:43:382023-07-06 07:44:02

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:44:02]
  • Judged
  • Verdict: TL
  • Time: 419ms
  • Memory: 308284kb
  • [2023-07-06 07:43:38]
  • 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 > 2)
//            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]);
//                    if(n == 717331 && col > 2)
//                        cout << Id[x] << ' ' << Id[to] << '\n';
                }
        for(int j = 1; j <= col; j++)
            D[j] = 0;
        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;
//        if(n == 717331 && col > 2)
//            cout << "DIAM = " << top << '\n';
        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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 27ms
memory: 259480kb

input:

4 3
0 1
1 2
2 0

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 32ms
memory: 261540kb

input:

4 2
0 1
1 2

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 313ms
memory: 308284kb

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: 219ms
memory: 287596kb

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: 77ms
memory: 308120kb

input:

946158 0

output:

946157

result:

ok 1 number(s): "946157"

Test #6:

score: 0
Accepted
time: 419ms
memory: 296708kb

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:

717330

result:

ok 1 number(s): "717330"

Test #7:

score: -100
Time Limit Exceeded

input:

1000000 1000000
0 168067
0 116972
1 776624
1 327773
1 145060
2 14506
2 181311
2 431159
2 652430
3 173884
4 394795
4 172555
5 494172
5 945490
7 595196
7 593371
7 358866
8 860794
9 598459
10 505821
10 41753
10 880384
11 74592
11 715116
11 659129
12 977041
12 807365
12 952349
13 844571
13 154379
15 185...

output:


result: