답안 #422470

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
422470 2021-06-10T07:07:52 Z Hideo 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
9 ms 15524 KB
#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define pi pair < int, int >

const int N = 3e5 + 7;
const int INF = 1e9 + 7;

ll sum;
int p[N];
int n, m;

map < pi, int > mp;
set < int > g[N];

int findp (int x){
    if (p[x] < 0)
        return x;
    return p[x] = findp(p[x]);
}

void unite (int v, int u){
    v = findp(v);
    u = findp(u);
    if (v != u){
        sum -= 1ll * int(g[u].size()) * -p[u];
        sum -= 1ll * int(g[v].size()) * -p[v];
        if (p[u] < p[v])
            swap(v, u);
        for (int to : g[u])
            g[v].insert(to);
        p[v] += p[u];
        p[u] = v;
        sum += 1ll * int(g[v].size()) * -p[v];
    }
}

main (){
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);
    memset(p, -1, sizeof(p));
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        if (mp[{b, a}]){
            unite(a, b);
        }
        else {
            sum -= 1ll * int(g[b].size()) * -p[b];
            g[b].insert(a);
            sum += 1ll * int(g[b].size()) * -p[b];
        }
        mp[{a, b}] = 1;
        cout << sum << endl;
    }
}

Compilation message

joitter2.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main (){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 15524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 15524 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 9 ms 15524 KB Output isn't correct
2 Halted 0 ms 0 KB -