답안 #1052589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1052589 2024-08-10T17:15:49 Z doducanh 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
100 / 100
558 ms 80472 KB
///breaker
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define ii pair<int,int>
#define mp make_pair
#define in(x) freopen(x,"r",stdin)
#define out(x) freopen(x,"w",stdout)
#define bit(x,i) ((x>>i)&1)
const int maxn=1e5+7;
int r[maxn];
int sz[maxn];
set<int>child[maxn],adj[maxn],radj[maxn];
queue<ii>q;
ll ans=0;
int n,t;
void insert_weak_connection(int x,int y)
{
    adj[x].insert(y);
    radj[y].insert(x);
    if(adj[y].count(x)){
        q.push({x,y});
    }
}
int Find(int u)
{
    return (u==r[u])?u:r[u]=Find(r[u]);
}
int dsu_size(int u)
{
    return adj[u].size()+child[u].size()+radj[u].size();
}
void Union(int a,int b)
{
    if(a==b)return;
    if(dsu_size(a)<dsu_size(b))swap(a,b);
    ans+=(1ll*sz[a]*child[b].size()+1ll*sz[b]*child[a].size());
    sz[a]+=sz[b];
    r[b]=a;
    for(int i:child[b]){
        if(child[a].count(i))ans-=sz[a];
        else child[a].insert(i);
    }
    for(int i:adj[b]){
        radj[i].erase(b);
        insert_weak_connection(a,i);
    }
    for(int i:radj[b]){
        adj[i].erase(b);
        insert_weak_connection(i,a);
    }
}
void sol(){
    cin>>n>>t;
    for(int i=1;i<=n;i++){
        r[i]=i;
        sz[i]=1;
        child[i].insert(i);
    }
    while(t--){
        int x,y;
        cin>>x>>y;
        y=Find(y);
        if(Find(x)!=y&&!child[y].count(x)){
            child[y].insert(x);
            ans+=sz[y];
            insert_weak_connection(Find(x),y);
            while(q.size()){
                auto[x,y]=q.front();
                q.pop();
                Union(Find(x),Find(y));
            }
        }
        cout<<ans<<"\n";
    }
}
main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    while(t--){
        sol();
    }
    // you should actually read the stuff at the bottom
    return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */

Compilation message

joitter2.cpp: In function 'void sol()':
joitter2.cpp:71:21: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |                 auto[x,y]=q.front();
      |                     ^
joitter2.cpp: At global scope:
joitter2.cpp:79:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   79 | main()
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 14964 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14940 KB Output is correct
14 Correct 2 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14960 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 2 ms 14940 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 2 ms 14940 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14940 KB Output is correct
23 Correct 2 ms 14940 KB Output is correct
24 Correct 2 ms 14940 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 2 ms 14940 KB Output is correct
27 Correct 2 ms 14936 KB Output is correct
28 Correct 2 ms 14936 KB Output is correct
29 Correct 2 ms 15020 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 2 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 14964 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14940 KB Output is correct
14 Correct 2 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14960 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 2 ms 14940 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 2 ms 14940 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14940 KB Output is correct
23 Correct 2 ms 14940 KB Output is correct
24 Correct 2 ms 14940 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 2 ms 14940 KB Output is correct
27 Correct 2 ms 14936 KB Output is correct
28 Correct 2 ms 14936 KB Output is correct
29 Correct 2 ms 15020 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 2 ms 14936 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 41 ms 20820 KB Output is correct
36 Correct 57 ms 23632 KB Output is correct
37 Correct 57 ms 23580 KB Output is correct
38 Correct 54 ms 23376 KB Output is correct
39 Correct 4 ms 15708 KB Output is correct
40 Correct 4 ms 15780 KB Output is correct
41 Correct 4 ms 15708 KB Output is correct
42 Correct 3 ms 15452 KB Output is correct
43 Correct 4 ms 15452 KB Output is correct
44 Correct 4 ms 15492 KB Output is correct
45 Correct 3 ms 15452 KB Output is correct
46 Correct 4 ms 15452 KB Output is correct
47 Correct 4 ms 15724 KB Output is correct
48 Correct 4 ms 15708 KB Output is correct
49 Correct 9 ms 16216 KB Output is correct
50 Correct 60 ms 23892 KB Output is correct
51 Correct 6 ms 15708 KB Output is correct
52 Correct 49 ms 22004 KB Output is correct
53 Correct 8 ms 16216 KB Output is correct
54 Correct 52 ms 22752 KB Output is correct
55 Correct 5 ms 15776 KB Output is correct
56 Correct 6 ms 15740 KB Output is correct
57 Correct 5 ms 15708 KB Output is correct
58 Correct 5 ms 15708 KB Output is correct
59 Correct 5 ms 15964 KB Output is correct
60 Correct 39 ms 20692 KB Output is correct
61 Correct 5 ms 15708 KB Output is correct
62 Correct 56 ms 23040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 2 ms 14940 KB Output is correct
3 Correct 2 ms 14940 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 2 ms 14940 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 2 ms 14940 KB Output is correct
11 Correct 2 ms 14964 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14940 KB Output is correct
14 Correct 2 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14960 KB Output is correct
17 Correct 2 ms 14940 KB Output is correct
18 Correct 2 ms 14940 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 2 ms 14940 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14940 KB Output is correct
23 Correct 2 ms 14940 KB Output is correct
24 Correct 2 ms 14940 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 2 ms 14940 KB Output is correct
27 Correct 2 ms 14936 KB Output is correct
28 Correct 2 ms 14936 KB Output is correct
29 Correct 2 ms 15020 KB Output is correct
30 Correct 2 ms 14940 KB Output is correct
31 Correct 2 ms 14940 KB Output is correct
32 Correct 2 ms 14940 KB Output is correct
33 Correct 2 ms 14936 KB Output is correct
34 Correct 3 ms 14940 KB Output is correct
35 Correct 41 ms 20820 KB Output is correct
36 Correct 57 ms 23632 KB Output is correct
37 Correct 57 ms 23580 KB Output is correct
38 Correct 54 ms 23376 KB Output is correct
39 Correct 4 ms 15708 KB Output is correct
40 Correct 4 ms 15780 KB Output is correct
41 Correct 4 ms 15708 KB Output is correct
42 Correct 3 ms 15452 KB Output is correct
43 Correct 4 ms 15452 KB Output is correct
44 Correct 4 ms 15492 KB Output is correct
45 Correct 3 ms 15452 KB Output is correct
46 Correct 4 ms 15452 KB Output is correct
47 Correct 4 ms 15724 KB Output is correct
48 Correct 4 ms 15708 KB Output is correct
49 Correct 9 ms 16216 KB Output is correct
50 Correct 60 ms 23892 KB Output is correct
51 Correct 6 ms 15708 KB Output is correct
52 Correct 49 ms 22004 KB Output is correct
53 Correct 8 ms 16216 KB Output is correct
54 Correct 52 ms 22752 KB Output is correct
55 Correct 5 ms 15776 KB Output is correct
56 Correct 6 ms 15740 KB Output is correct
57 Correct 5 ms 15708 KB Output is correct
58 Correct 5 ms 15708 KB Output is correct
59 Correct 5 ms 15964 KB Output is correct
60 Correct 39 ms 20692 KB Output is correct
61 Correct 5 ms 15708 KB Output is correct
62 Correct 56 ms 23040 KB Output is correct
63 Correct 277 ms 67676 KB Output is correct
64 Correct 269 ms 67668 KB Output is correct
65 Correct 280 ms 67668 KB Output is correct
66 Correct 127 ms 43204 KB Output is correct
67 Correct 259 ms 71760 KB Output is correct
68 Correct 126 ms 43088 KB Output is correct
69 Correct 237 ms 43092 KB Output is correct
70 Correct 156 ms 43056 KB Output is correct
71 Correct 155 ms 43088 KB Output is correct
72 Correct 243 ms 55496 KB Output is correct
73 Correct 284 ms 57080 KB Output is correct
74 Correct 558 ms 78912 KB Output is correct
75 Correct 328 ms 49452 KB Output is correct
76 Correct 401 ms 62396 KB Output is correct
77 Correct 437 ms 62732 KB Output is correct
78 Correct 130 ms 42072 KB Output is correct
79 Correct 247 ms 49744 KB Output is correct
80 Correct 123 ms 41040 KB Output is correct
81 Correct 201 ms 47008 KB Output is correct
82 Correct 461 ms 57948 KB Output is correct
83 Correct 454 ms 57684 KB Output is correct
84 Correct 362 ms 56820 KB Output is correct
85 Correct 379 ms 56740 KB Output is correct
86 Correct 217 ms 78252 KB Output is correct
87 Correct 221 ms 80472 KB Output is correct
88 Correct 261 ms 56400 KB Output is correct
89 Correct 414 ms 61224 KB Output is correct