QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#245560#5492. Toll Roadsmomen159#AC ✓296ms71796kbC++144.4kb2023-11-10 01:57:432023-11-10 01:57:44

Judging History

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

  • [2023-11-10 01:57:44]
  • 评测
  • 测评结果:AC
  • 用时:296ms
  • 内存:71796kb
  • [2023-11-10 01:57:43]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
#define ll long long
#define momen ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define ld long double
#define fp(n) for(int i=0;i<n;i++)
#define fp1(n) for(int i=1;i<=n;i++)
#define all(v) v.begin() , v.end()
const int mod = 1e9 + 7, N = 2e5 + 5, M = 8e6 + 5;
const ll LG = 20, inf = 1e9 + 6;


int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};


pair<int,int> anc[N][20], p[N] ;
int d[N], n, q ;
vector<pair<int ,int>> adj[N];

void dfs(int i, int par , int c)
{
    p[i] = {par ,c};
    d[i] = d[par]+1;
    for(auto e:adj[i])
    {
        if(e.first == par)
            continue;
        dfs(e.first, i , e.second);
    }
}

//need par, depth
void preprocess()
{
    // k is 2^k anc
    for(int k=0;k<LG; k++)
    {
        for(int i=1; i<=n; i++)
        {
            if(k == 0)
                anc[i][k] = p[i];
            else {
                anc[i][k].first = anc[anc[i][k - 1].first][k - 1].first;
                anc[i][k].second = max(anc[anc[i][k - 1].first][k - 1].second, anc[i][k - 1].second);
            }
        }
    }
}

pair<int ,int> binaryLift(int x, int jump)
{
    int mx  =  0 ;
    for(int b=0; b<LG; b++)
    {
        if( jump & (1 << b)) {
            mx = max(mx , anc[x][b].second ) ;
            x = anc[x][b].first;
        }
    }
    return {x, mx};
}
int LCA(int a, int b)
{
    if(d[a] > d[b])
        swap(a, b);

    // guaranteed that b is deeper
    //make same depth
    int diff = d[b] - d[a];
    int mx = 0 ;
    tie(b,mx) = binaryLift(b, diff);

    if(a == b)
        return mx;

    for(int bit=LG-1; bit>=0; bit--)
    {
        if(anc[a][bit].first == anc[b][bit].first)
            continue;
        mx = max({mx ,anc[a][bit].second , anc[b][bit].second}) ;
        a = anc[a][bit].first;
        b = anc[b][bit].first;
    }
    return max({mx, p[a].second , p[b].second});
}



struct DSU{
    vector<int>par ;
    vector<int>sze ;
    int mx = 1;
    DSU(int n){
        par.resize(n+1) ;
        sze.resize(n+1) ;
        fp(n+1) {
            par[i]= i ;
            sze[i]= 1;
        }
    }
    int find(int a){
        while(par[a] != a ){
            a = par[a] ;
        }
        return a;
    }
    bool unite(int a , int b ){
        a = find(a) ;
        b = find(b) ;
        if (a==b)
            return 1 ;
        if (sze[a] < sze[b])
            swap(a,b) ;
        par[b] = par[a] ;
        sze[a]+=sze[b] ;
        return 0 ;
    }
};



void solve(int z) {
    int m , q;
    cin>>n>>m>>q;
    vector<pair<int ,pair<int ,int>>> edges ;
    for (int i = 0; i < m; ++i) {
        int a,b,c ;
        cin>>a>>b>>c;
        edges.push_back({c,{a,b}}) ;
    }
    sort(all(edges)) ;
    DSU d(n+1) ;
    for (int i = 0; i < m; ++i) {
        int a ,b,c ;
        c = edges[i].first , a = edges[i].second.first , b = edges[i].second.second;
        if (d.unite(a,b))
            continue;
        adj[a].push_back({b,c}) ,adj[b].push_back({a,c}) ;
    }
    dfs(1,0,0) ;
    preprocess() ;

    vector<pair<int ,int>>qu[N] ;

    for (int i = 1; i <= q; ++i) {
        int a,b ;
        cin>>a>>b ;
        int x = LCA(a,b) ;
        qu[x].push_back({i , a}) ;
    }
    DSU d2(n+1) ;
    vector<pair<int ,int>>ans(q+1) ;

    for (int i = 0; i < m; ++i) {
        int a ,b,c ;
        c = edges[i].first , a = edges[i].second.first , b = edges[i].second.second;
        if (i && c > edges[i-1].first){
            for (int j = 0; j < qu[edges[i-1].first].size(); ++j) {
                int z = qu[edges[i-1].first][j].second ;
                int y = d2.find(z) ;
                int size = d2.sze[y] ;
                ans[qu[edges[i-1].first][j].first] = {edges[i-1].first ,size };
            }
        }
        if (d2.unite(a,b))
            continue;
    }
    for (int j = 0; j < qu[edges.back().first].size(); ++j) {
        ans[qu[edges.back().first][j].first] = {edges.back().first ,n};
    }

    for (int i = 1; i <=q ; ++i) {
        cout<<ans[i].first<<" "<<ans[i].second<<endl ;
    }

}


int main() {
    momen
    int t = 1;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
//    cin >> t;

    for (int i = 1; i <= t; ++i) {
        //cout<<"Case #"<<i<<": ";
        solve(t);
    }

    return 0;
}

详细

Test #1:

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

input:

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

output:

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

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 200ms
memory: 69660kb

input:

200000 199999 200000
40203 148773 165038
148773 54931 77889
54931 9912 192188
9912 180491 24730
180491 77332 36976
77332 43929 146532
43929 43341 172446
43341 141304 121793
141304 105828 148202
105828 72010 107746
72010 152016 156358
152016 150074 115703
150074 117105 73900
117105 57831 59264
57831 ...

output:

165038 3
77889 2
192188 41
24730 2
36976 3
146532 4
172446 20
121793 2
148202 4
107746 2
156358 10
115703 6
73900 5
59264 2
67443 4
26999 2
156979 16
80963 3
56618 2
107615 6
63765 3
19719 2
178439 35
95141 5
72029 4
14650 2
21437 3
109944 6
139220 7
141978 9
102949 2
170997 15
100704 3
75249 2
1312...

result:

ok 200000 lines

Test #3:

score: 0
Accepted
time: 189ms
memory: 68056kb

input:

200000 199999 200000
25566 162429 116811
162429 150239 28436
150239 75366 140258
75366 176680 111452
176680 158813 50710
158813 125248 118834
125248 191706 31210
191706 163115 65321
163115 46167 44831
46167 129128 79156
129128 112971 51160
112971 195397 1773
195397 196884 159329
196884 188278 191759...

output:

116811 3
28436 2
140258 13
118834 10
118834 10
118834 10
191759 21
159329 14
79156 7
159329 14
191759 21
159329 14
159329 14
191759 21
95051 2
197843 41
46441 2
197843 41
197843 41
197843 41
197843 41
179891 15
173651 10
132452 7
179891 15
84295 3
179891 15
173651 10
179891 15
179891 15
179891 15
18...

result:

ok 200000 lines

Test #4:

score: 0
Accepted
time: 296ms
memory: 66232kb

input:

200000 199999 200000
108049 140092 177196
140092 142782 104903
142782 144052 130828
144052 105524 197299
105524 146641 105000
146641 126120 6587
126120 23329 185363
23329 145087 162872
145087 189421 141021
189421 96046 167286
96046 67072 147548
67072 7773 150238
7773 157376 141227
157376 3589 103113...

output:

199998 166739
199998 166739
199997 137953
199999 200000
199961 33049
199991 65901
199995 31759
199999 200000
199991 65901
199998 166739
199997 137953
199998 166739
199999 200000
199997 137953
199999 200000
199978 17913
199997 137953
199981 18397
199997 137953
199990 21472
199927 1556
199998 166739
1...

result:

ok 200000 lines

Test #5:

score: 0
Accepted
time: 31ms
memory: 15960kb

input:

2 1 200000
2 1 0
1 2
1 2
2 1
2 1
2 1
2 1
2 1
1 2
2 1
2 1
1 2
1 2
1 2
1 2
1 2
2 1
1 2
2 1
2 1
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
1 2
2 1
1 2
1 2
1 2
1 2
1 2
2 1
2 1
1 2
2 1
1 2
2 1
1 2
2 1
1 2
1 2
2 1
1 2
1 2
1 2
2 1
1 2
1 2
1 2
2 1
2 1
1 2
2 1
1 2
1 2
2 1
1 2
1 2
2 1
2 1
2 1
1 2
2 1
2 1
2 1
1 2
1 2
1 2...

output:

0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
0 2
...

result:

ok 200000 lines

Test #6:

score: 0
Accepted
time: 28ms
memory: 15884kb

input:

2 1 200000
2 1 200000
2 1
1 2
1 2
2 1
2 1
1 2
1 2
1 2
1 2
1 2
2 1
2 1
2 1
1 2
1 2
1 2
1 2
1 2
1 2
2 1
2 1
2 1
2 1
1 2
1 2
2 1
1 2
2 1
2 1
2 1
1 2
2 1
2 1
1 2
1 2
1 2
1 2
2 1
2 1
2 1
2 1
1 2
1 2
2 1
1 2
2 1
2 1
1 2
2 1
1 2
1 2
2 1
2 1
1 2
2 1
1 2
2 1
1 2
2 1
1 2
2 1
2 1
1 2
2 1
1 2
2 1
2 1
1 2
1 2
1 ...

output:

200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200000 2
200...

result:

ok 200000 lines

Test #7:

score: 0
Accepted
time: 192ms
memory: 71244kb

input:

200000 199999 199999
107673 108777 137731
161326 2822 185976
42519 186329 11368
2875 128189 40207
660 17140 174600
80080 45700 24002
103478 55840 94897
141771 83861 106266
190280 61753 130887
100340 51796 36679
85897 157537 68633
179542 133431 726
4727 100325 68158
196602 138158 15657
61216 85366 13...

output:

182636 182638
188210 188212
102022 102024
92251 92253
156363 156365
56753 56755
160474 160476
43371 43373
78661 78663
102391 102393
87733 87735
50624 50626
167931 167933
163802 163804
30413 30415
9548 9550
125354 125356
66593 66595
12746 12748
172788 172790
150755 150757
175783 175785
198133 198135
...

result:

ok 199999 lines

Test #8:

score: 0
Accepted
time: 86ms
memory: 18792kb

input:

632 199396 199396
528 623 151485
126 300 71970
423 596 177223
239 363 43731
126 129 43760
167 445 129222
81 349 53617
255 594 20913
222 468 187290
40 424 196898
182 202 97262
57 276 187115
20 125 84603
196 281 60208
91 159 68556
70 370 62697
64 519 173483
132 525 149142
501 581 7624
423 523 151709
9...

output:

383 254
518 456
986 600
740 559
391 328
716 551
312 18
439 379
530 469
1365 623
678 539
522 464
1003 604
498 437
1245 618
531 472
357 196
465 410
363 218
380 244
983 599
487 427
417 353
566 487
640 529
382 247
361 199
678 539
531 472
732 558
1006 605
391 328
344 181
570 490
436 377
439 379
363 218
6...

result:

ok 199396 lines

Test #9:

score: 0
Accepted
time: 173ms
memory: 40880kb

input:

100000 200000 200000
52858 70914 35062
99437 57982 31435
95724 61978 154826
80071 97428 180435
37559 41986 186600
82330 60710 46299
50361 2041 11824
97939 7216 68467
27360 20298 20367
22901 33604 152232
85363 46295 36133
7948 46833 105272
62600 12092 16575
68460 39871 29788
88827 76407 41346
84845 7...

output:

65422 31036
156936 98628
119689 91378
82807 65139
70554 43803
163978 99182
103626 84209
66822 34554
88936 72340
127128 93765
76755 56084
77303 56989
128035 94040
73388 50011
66219 32835
120774 91787
79431 60407
114700 89562
81951 64046
136949 96038
66867 34668
64709 28903
74431 51949
69879 42315
628...

result:

ok 200000 lines

Test #10:

score: 0
Accepted
time: 289ms
memory: 65436kb

input:

200000 199999 200000
185243 33784 95622
46781 107802 42086
149014 198625 45412
159192 1231 32912
68503 148649 106263
135161 27908 168574
180133 12562 165333
72353 160550 156077
4001 45858 48858
9451 6397 160416
160375 122402 112610
176893 162254 46641
199087 36919 21
111525 125696 147048
53720 73845...

output:

199997 195765
199997 195765
199979 33236
199997 195765
199997 195765
199997 195765
199997 195765
199997 195765
199991 83259
199976 39425
199997 195765
199985 73860
199836 6767
199976 39425
199997 195765
199997 195765
199995 107073
199999 200000
199995 107073
199997 195765
199991 83259
199997 195765
...

result:

ok 200000 lines

Test #11:

score: 0
Accepted
time: 196ms
memory: 63872kb

input:

200000 199999 200000
83491 93406 16020
160288 46393 68815
150261 39524 106233
89684 101484 186710
72901 167375 69517
145319 54171 130836
162903 176302 16129
125258 135455 35814
139578 70344 196425
57001 26850 8975
169239 113975 97460
148216 182789 49380
42991 128149 196748
166876 93306 88264
119844 ...

output:

194278 141013
194278 141013
198809 187567
196050 157285
191419 114070
199593 196267
198296 179544
174557 31900
198534 184764
177605 40570
190205 104254
181247 58841
188794 95616
198534 184764
198523 181455
189467 100962
198385 180269
198187 178863
197299 168100
189577 101808
196540 162472
194278 141...

result:

ok 200000 lines

Test #12:

score: 0
Accepted
time: 209ms
memory: 71796kb

input:

200000 199999 199999
143668 143669 143668
11617 11618 11617
37034 37035 37034
185348 185349 185348
136887 136888 136887
188027 188028 188027
103206 103207 103206
145144 145145 145144
67513 67514 67513
167655 167656 167655
187910 187911 187910
30298 30299 30298
162829 162830 162829
182915 182916 1829...

output:

146312 146313
160448 160449
100437 100438
54296 54297
160211 160212
59722 59723
117676 117677
178941 178942
49823 49824
63026 63027
90309 90310
35791 35792
171473 171474
62058 62059
74645 74646
128605 128606
17748 17749
166510 166511
100160 100161
43567 43568
67261 67262
163412 163413
164743 164744
...

result:

ok 199999 lines

Test #13:

score: 0
Accepted
time: 178ms
memory: 71752kb

input:

200000 199999 199999
183516 183517 16484
31945 31946 168055
187198 187199 12802
36772 36773 163228
15314 15315 184686
157786 157787 42214
195409 195410 4591
117281 117282 82719
100043 100044 99957
146665 146666 53335
99315 99316 100685
32933 32934 167067
197914 197915 2086
45372 45373 154628
101825 ...

output:

90837 90838
97994 97995
131658 131659
30073 30074
183244 183245
199094 199095
109081 109082
154046 154047
28222 28223
48271 48272
64951 64952
62521 62522
119663 119664
4898 4899
177650 177651
89865 89866
171354 171355
182452 182453
88348 88349
126662 126663
128475 128476
130436 130437
80054 80055
66...

result:

ok 199999 lines

Test #14:

score: 0
Accepted
time: 170ms
memory: 64904kb

input:

200000 199999 200000
537 134680 44756
92989 25467 154790
13052 50779 79699
166373 158353 130816
156718 74433 103072
128309 109704 100153
136756 179599 162791
78195 95533 171942
188130 4999 130076
22750 130455 80673
142159 104070 163798
179861 46359 113175
75257 124953 34160
178575 159252 187249
1736...

output:

174341 149634
147053 105841
191645 184741
142739 100177
185016 171148
103475 52785
197319 193762
170693 142439
164832 133195
185016 171148
140100 96192
183052 165345
187830 175382
168226 138890
124364 73840
85566 35859
173534 148362
191594 184115
180547 159882
147053 105841
168226 138890
145794 1036...

result:

ok 200000 lines

Test #15:

score: 0
Accepted
time: 156ms
memory: 41436kb

input:

100000 200000 200000
61665 95293 34999
92437 17782 30456
94649 79809 163444
90369 26124 111763
61665 59107 9953
61665 40182 69232
48130 75359 143570
59858 4110 70372
61665 83820 157303
39647 77218 46638
88075 59632 2355
72951 22972 159732
61665 72281 182488
70854 43327 166254
73926 9030 19560
90890 ...

output:

110869 82103
152800 94332
181924 98533
124286 87097
126509 87801
57899 45494
107467 80599
50946 38961
86954 69113
90847 71632
66580 53139
139648 91527
74771 60099
65603 52269
115982 84155
99002 76184
62114 49297
36851 26020
39292 28159
64938 51732
85874 68399
82468 66037
104909 79349
67818 54236
113...

result:

ok 200000 lines

Test #16:

score: 0
Accepted
time: 170ms
memory: 41180kb

input:

100000 200000 200000
38574 81368 165468
5958 89291 58685
21278 55089 149933
22326 84479 118868
64584 5391 10008
76431 45431 43872
34083 16278 101144
78564 96999 26280
10560 7649 9300
97899 75637 109231
58936 8153 106816
18689 58323 56855
40790 96223 112950
54298 14681 7483
45472 5207 152518
60778 90...

output:

91289 74907
91600 75142
100593 81356
84971 69101
70889 49463
144734 95667
61879 28843
86868 70979
130886 93115
93186 76405
111174 86751
66769 40765
78920 61966
135463 94093
65769 38139
110719 86551
89907 73802
138514 94685
81430 65163
63020 32155
77014 59446
82826 66730
143615 95500
62373 29897
7320...

result:

ok 200000 lines