답안 #869083

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
869083 2023-11-03T05:58:51 Z Leo121 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
100 / 100
543 ms 65384 KB
#include <bits/stdc++.h>

#define pb push_back
#define rbg(v) v.rbegin()
#define bg(v) v.begin()
#define all(v) bg(v), v.end()
#define sz(v) int(v.size())
#define mp make_pair
#define fi first
#define se second
#define forn(i, a, b) for(int i = int(a); i <= int(b); ++ i)
#define for0(i, n) forn(i, 0, n - 1)
#define for1(i, n) forn(i, 1, n)
#define fora(i, a, b) for(int i = int(a); i >= int(b); -- i)
#define far0(i, n) fora(i, n - 1, 0)
#define far1(i, n) fora(i, n, 1)
#define foru(i, v) for(auto & i : v)
#define lb lower_bound
#define ub upper_bound
#define sz(v) int(v.size())
#define ord1(s, n) s + 1, s + int(n) + 1
#define ord0(s, n) s, s + int(n)
#define debug(x) /// cout << #x << " = " << x << endl

using namespace std;

///#include <bits/extc++.h>
///using namespace __gnu_pbds;
///typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ost;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef long double ld;
typedef double db;
typedef long long lli;
///typedef __int128 Int;

const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
///const int inf = 1e4

const int MX = 1e5 + 2;

struct SML{
    map<int, int> in;
    map<int, int> out;
    set<int> in2;
    int sum_in;
    SML () {
        sum_in = 0;
    }
};

SML * comp[MX];
int p[MX], setSize[MX];
ll ori_edg, com_gra, adi_edg;
queue<pii> q;

void UnionFind(int N){
    for1(i, N) p[i] = i, setSize[i] = 1;
}

int findSet(int i){
    return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}

bool isSameSet(int i, int j){
    return findSet(i) == findSet(j);
}

void unionSet(int i, int j){
    if(isSameSet(i, j)) return;
    int x = findSet(i), y = findSet(j);
    if(comp[x]->in.size() + comp[x]->out.size() + comp[x]->in2.size() > comp[y]->in.size() + comp[y]->out.size() + comp[y]->in2.size()) swap(x, y);
    adi_edg -= ll(comp[y]->sum_in) * ll(setSize[y] - 1);
    adi_edg -= ll(comp[x]->sum_in) * ll(setSize[x] - 1);
    com_gra -= ll(setSize[x]) * ll(setSize[x] - 1);
    com_gra -= ll(setSize[y]) * ll(setSize[y] - 1);
    comp[y]->sum_in += comp[x]->sum_in;
    for(auto [key, val] : comp[x]->in){
        if(key == x) continue;
        if(key == y){
            comp[key]->out.erase(x);
            ori_edg -= val;
            comp[y]->sum_in -= val;
            continue;
        }
        comp[key]->out.erase(x);
        comp[key]->out[y] += val;
        if(comp[y]->out.count(key))
            q.push(mp(y, key));
        comp[y]->in[key] += val;
    }
    for(auto [key, val] : comp[x]->out){
        if(key == x) continue;
        if(key == y){
            comp[key]->in.erase(x);
            ori_edg -= val;
            comp[y]->sum_in -= val;
            continue;
        }
        comp[key]->in.erase(x);
        comp[key]->in[y] += val;
        if(comp[y]->in.count(key))
            q.push(mp(y, key));
        comp[y]->out[key] += val;
    }
    for(auto val : comp[x]->in2){
        int s = findSet(val);
        if(s == x) continue;
        if(s == y) continue;
        if(comp[y]->in2.count(val)){
            ori_edg --;
            comp[y]->sum_in --;
            comp[y]->in[s] --;
            comp[s]->out[y] --;
            continue;
        }
        comp[y]->in2.insert(val);
    }
    p[x] = y;
    setSize[y] += setSize[x];
    com_gra += ll(setSize[y]) * ll(setSize[y] - 1);
    adi_edg += ll(comp[y]->sum_in) * ll(setSize[y] - 1);
}

void answer(){
    cout << ori_edg + adi_edg + com_gra << '\n';
}

void solve(){
    int n, m;
    cin >> n >> m;
    for1(i, n) comp[i] = new SML();
    UnionFind(n);
    while(m --){
        int u, v;
        cin >> u >> v;
        if(isSameSet(u, v)){
            answer();
            continue;
        }
        int x, y;
        x = findSet(u), y = findSet(v);
        if(!comp[x]->in.count(y)){
            if(comp[y]->in2.count(u)){
                answer();
                continue;
            }
            ori_edg ++;
            adi_edg += ll(setSize[y] - 1);
            comp[y]->sum_in ++;
            comp[y]->in[x] ++;
            comp[x]->out[y] ++;
            comp[y]->in2.insert(u);
            answer();
            continue;
        }
        unionSet(u, v);
        while(!q.empty()){
            unionSet(q.front().fi, q.front().se);
            q.pop();
        }
        answer();
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t = 1;
    ///cin >> t;
    for1(i, t){
        ///cout << "Case #" << i << ": ";
        solve();
        ///cout << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 0 ms 464 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 0 ms 464 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 600 KB Output is correct
35 Correct 54 ms 6224 KB Output is correct
36 Correct 73 ms 9296 KB Output is correct
37 Correct 72 ms 9300 KB Output is correct
38 Correct 70 ms 9044 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 1116 KB Output is correct
41 Correct 2 ms 984 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 2 ms 992 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 860 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 2 ms 992 KB Output is correct
49 Correct 8 ms 1884 KB Output is correct
50 Correct 73 ms 9336 KB Output is correct
51 Correct 5 ms 1368 KB Output is correct
52 Correct 62 ms 7768 KB Output is correct
53 Correct 7 ms 1884 KB Output is correct
54 Correct 69 ms 8600 KB Output is correct
55 Correct 4 ms 1372 KB Output is correct
56 Correct 4 ms 1372 KB Output is correct
57 Correct 4 ms 1372 KB Output is correct
58 Correct 4 ms 1372 KB Output is correct
59 Correct 2 ms 860 KB Output is correct
60 Correct 54 ms 5768 KB Output is correct
61 Correct 3 ms 1116 KB Output is correct
62 Correct 69 ms 8720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 468 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 0 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 600 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 464 KB Output is correct
24 Correct 0 ms 464 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 2 ms 600 KB Output is correct
35 Correct 54 ms 6224 KB Output is correct
36 Correct 73 ms 9296 KB Output is correct
37 Correct 72 ms 9300 KB Output is correct
38 Correct 70 ms 9044 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 1116 KB Output is correct
41 Correct 2 ms 984 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 2 ms 992 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 860 KB Output is correct
47 Correct 2 ms 1116 KB Output is correct
48 Correct 2 ms 992 KB Output is correct
49 Correct 8 ms 1884 KB Output is correct
50 Correct 73 ms 9336 KB Output is correct
51 Correct 5 ms 1368 KB Output is correct
52 Correct 62 ms 7768 KB Output is correct
53 Correct 7 ms 1884 KB Output is correct
54 Correct 69 ms 8600 KB Output is correct
55 Correct 4 ms 1372 KB Output is correct
56 Correct 4 ms 1372 KB Output is correct
57 Correct 4 ms 1372 KB Output is correct
58 Correct 4 ms 1372 KB Output is correct
59 Correct 2 ms 860 KB Output is correct
60 Correct 54 ms 5768 KB Output is correct
61 Correct 3 ms 1116 KB Output is correct
62 Correct 69 ms 8720 KB Output is correct
63 Correct 267 ms 65244 KB Output is correct
64 Correct 250 ms 65384 KB Output is correct
65 Correct 261 ms 65324 KB Output is correct
66 Correct 75 ms 31316 KB Output is correct
67 Correct 152 ms 34812 KB Output is correct
68 Correct 86 ms 31280 KB Output is correct
69 Correct 172 ms 32592 KB Output is correct
70 Correct 75 ms 31404 KB Output is correct
71 Correct 88 ms 31312 KB Output is correct
72 Correct 162 ms 34912 KB Output is correct
73 Correct 160 ms 35044 KB Output is correct
74 Correct 543 ms 62572 KB Output is correct
75 Correct 343 ms 40240 KB Output is correct
76 Correct 379 ms 50336 KB Output is correct
77 Correct 407 ms 50616 KB Output is correct
78 Correct 133 ms 37424 KB Output is correct
79 Correct 233 ms 42336 KB Output is correct
80 Correct 123 ms 36124 KB Output is correct
81 Correct 193 ms 40004 KB Output is correct
82 Correct 448 ms 55208 KB Output is correct
83 Correct 449 ms 55368 KB Output is correct
84 Correct 342 ms 54352 KB Output is correct
85 Correct 337 ms 54352 KB Output is correct
86 Correct 95 ms 30584 KB Output is correct
87 Correct 105 ms 32748 KB Output is correct
88 Correct 171 ms 35384 KB Output is correct
89 Correct 352 ms 49232 KB Output is correct