Submission #383269

# Submission time Handle Problem Language Result Execution time Memory
383269 2021-03-29T11:29:11 Z VodkaInTheJar Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
1 ms 364 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 1e5 + 3; 

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

    return par[ver] = find_root(par[ver]);
}

void merge(int a, int b)
{
    a = find_root(a), b = find_root(b);
    if (sz[a] < sz[b])
    swap(a, b);

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

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

vector <pair <int, int> > v; 

long long find_ans()
{
    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 (auto i: v)
    {
        int a = find_root(i.first), b = find_root(i.second);
        if (a == b)
            continue;

        if (s.find({i.first, b}) != s.end())
            continue;

        ans += sz[b];
        s.insert({i.first, b});
    }

    return ans; 
}

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

    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;

        if (find_root(a) != find_root(b))
        {
            v.push_back({a, b});
            for (auto i: v)
                if (find_root(i.first) == find_root(b) && find_root(i.second) == find_root(a))
                {
                    merge(a, b);
                    break;
                }
        }
        
        cout << find_ans() << endl; 
    }
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	
	read();
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Incorrect 1 ms 364 KB Output isn't correct
4 Halted 0 ms 0 KB -