답안 #384067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
384067 2021-03-31T10:28:52 Z VodkaInTheJar 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
0 / 100
2 ms 364 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 1e5 + 3; 

int n, m; 
void read()
{
	cin >> n >> m; 
}

int par[maxn], sz[maxn];
int find_root(int ver)
{
    if (ver == par[ver])
        return ver;

    return find_root(par[ver]);
}

void merge(int a, int b)
{
    a = find_root(a), b = find_root(b);
    
    if (a == b)
        return;

    if (sz[a] < sz[b])
        swap(a, b);

    par[b] = a;
    sz[a] += sz[b];
}

pair <int, int> e[maxn * 3];
long long find_ans(int idx)
{
    long long ans = 0; 
    for (int i = 1; i <= n; i++)
        if (i == par[i])
            ans += 1ll * sz[i] * (sz[i] - 1); 

    set <pair <int, int> > s; 
    for (int i = 1; i <= idx; i++)
        if (find_root(e[i].first) != find_root(e[i].second))
        {
            s.insert({e[i].first, find_root(e[i].second)});
            //ans += sz[find_root(e[i].second)];
        }

    for (auto i: s)
        ans += (long long)sz[i.second];

    return ans; 
}

void solve()
{
    for (int i = 1; i <= n; i++)
        par[i] = i, sz[i] = 1; 

    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].first >> e[i].second;
        
        if (find_root(e[i].first) != find_root(e[i].second))
        {
            for (int j = 1; j <= i-1; j++)
                if (find_root(e[j].second) == find_root(e[i].first) && find_root(e[j].first) == find_root(e[i].second))
                { 
                    merge(e[i].first, e[i].second);
                    break;
                }
        }

        cout << find_ans(i) << endl; 
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	read();
	solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 2 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 2 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 2 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -