QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#710767 | #3556. Making Friends on Joitter is Fun | sitablechair | 0 | 4ms | 47520kb | C++23 | 2.9kb | 2024-11-04 21:33:55 | 2024-11-04 21:33:58 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=3e5+3;
int n,m,f[N],f1[N],mmb[N];
ll ans=0;
map<int,int> mp[N],mp1[N];
set<int> st[N];
vector<int> ahu;
queue<pair<int,int>> q;
int find_set(int u) {
return (f[u]<0?u:f[u]=find_set(f[u]));
}
int find_set1(int u) {
return (f1[u]<0?u:f1[u]=find_set1(f1[u]));
}
void unite(int u,int v) {
int a=find_set(u),b=find_set(v);
if (a==b) return;
int a1=find_set1(u),b1=find_set1(v);
ans+=1LL*f[a]*(sz(st[a1])-1);
ans+=1LL*f[b]*(sz(st[b1])-1);
if (sz(st[a1])<sz(st[b1])) swap(a1,b1);
if (sz(mp[a])+sz(mp1[a])<sz(mp[b])+sz(mp1[b])) swap(a,b);
ahu.clear();
for(auto el: mp[b]) {
ahu.pb(el.ff);
if (mp[find_set(el.ff)].count(a) || mp[find_set(el.ff)].count(b)) q.push({el.ff,a});
}
for(auto el: mp1[b])
if (mp1[el.ff].count(a) || mp1[el.ff].count(b)) q.push({el.ff,a});
for(auto el: mp1[b1]) {
mp[el.ff][a]=mp[el.ff][b];
// mp[el.ff].erase(mp[el.ff].find(b));
}
for(auto el: ahu) {
mp1[el][a]=mp1[el][b];
// mp1[el].erase(mp1[el].find(b));
}
for(auto el: st[b1]) st[a1].insert(el);
st[b1].clear();
for(auto el: mp[b]) mp[a][el.ff]+=el.ss;
mp[b].clear();
for(auto el: mp1[b]) mp1[a][el.ff]+=el.ss;
mp1[b].clear();
f[a]+=f[b];
f1[a1]+=f1[b1];
f[b]=a;
f1[b1]=a1;
ans-=1LL*f[a]*(sz(st[find_set1(a)])-1);
// for(auto el: ahu1[a]) {
// if (find_set(el)==find_set(a)) continue;
// unite(el,a);
// }
// ahu1[a].clear();
}
void add_edge(int u,int v) {
if (find_set(u)==find_set(v) || st[find_set1(v)].find(u)!=st[find_set1(v)].end()) return;
mp[find_set(v)][find_set(u)]++;
mp1[find_set(u)][find_set(v)]++;
if (st[find_set1(v)].find(u)==st[find_set1(v)].end()) ans-=f[find_set(v)];
st[find_set1(v)].insert(u);
if (mp[find_set(u)].count(find_set(v))) unite(u,v);
while(sz(q)) {
unite(q.front().ff,q.front().ss);
q.pop();
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
// freopen("B.INP","r",stdin);
// freopen("B.OUT","w",stdout);
cin >> n >> m;
For(i,1,n) f[i]=f1[i]=-1;
For(i,1,n) st[i].insert(i);
For(i,1,m) {
int u,v;
cin >> u >> v;
add_edge(u,v);
cout << ans << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 3ms
memory: 47488kb
input:
5 20 4 2 1 5 2 3 2 5 3 2 3 1 4 5 1 2 5 2 1 4 4 1 3 4 5 1 2 4 2 1 4 3 1 3 5 4 3 5 5 3
output:
1 2 3 4 6 7 8 12 16 20 20 20 20 20 20 20 20 20 20 20
result:
ok 20 lines
Test #2:
score: 1
Accepted
time: 4ms
memory: 46260kb
input:
5 20 4 5 1 2 2 1 4 3 3 2 5 2 1 5 4 1 2 3 1 3 1 4 4 2 5 1 3 5 2 5 3 1 2 4 3 4 5 4 5 3
output:
1 2 3 4 6 8 13 13 16 16 20 20 20 20 20 20 20 20 20 20
result:
ok 20 lines
Test #3:
score: 1
Accepted
time: 3ms
memory: 46720kb
input:
5 20 3 1 5 1 3 4 5 2 1 2 5 4 3 5 2 4 1 3 2 5 4 5 4 3 4 2 2 3 3 2 5 3 2 1 1 5 4 1 1 4
output:
1 2 3 4 5 6 7 8 11 15 20 20 20 20 20 20 20 20 20 20
result:
ok 20 lines
Test #4:
score: 1
Accepted
time: 4ms
memory: 47196kb
input:
10 10 9 1 10 6 4 10 1 8 4 9 7 8 2 4 6 5 10 1 1 7
output:
1 2 3 4 5 6 7 8 9 10
result:
ok 10 lines
Test #5:
score: 1
Accepted
time: 3ms
memory: 46152kb
input:
10 90 10 6 7 8 8 4 9 3 2 8 9 2 1 10 1 8 8 9 5 6 2 10 4 3 7 2 10 2 10 5 3 7 1 9 8 7 1 2 9 1 7 6 9 7 10 3 8 3 6 3 7 5 8 2 6 1 4 9 7 1 4 2 6 8 7 3 9 8 5 1 10 4 6 4 1 4 6 7 3 1 9 10 3 2 1 7 2 5 10 1 2 7 2 1 5 8 7 9 2 4 6 9 3 8 10 7 8 5 5 4 8 6 5 10 3 5 5 7 8 1 4 7 4 1 10 8 9 5 4 8 6 10 1 6 2 9 1 5 6 5 3...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 17 18 19 20 52 52 59 60 60 60 60 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
result:
ok 90 lines
Test #6:
score: 1
Accepted
time: 4ms
memory: 47360kb
input:
10 90 10 9 7 3 4 7 8 1 10 8 4 10 9 7 9 6 2 3 1 8 5 6 1 2 10 5 3 2 6 5 6 3 9 2 4 6 9 3 9 1 5 7 6 10 10 1 8 9 9 8 5 4 10 4 5 1 2 9 7 9 9 5 7 6 9 10 4 8 5 3 2 7 8 2 6 8 7 2 2 8 1 10 1 9 8 5 3 6 4 1 7 1 10 7 8 3 2 10 5 8 7 10 1 6 3 5 2 4 6 4 8 4 1 7 6 2 7 4 2 1 3 7 7 8 1 5 8 10 4 5 5 10 5 2 3 8 5 9 6 1 ...
output:
1 2 3 4 5 6 7 8 9 11 12 13 14 17 20 22 24 26 26 28 29 35 35 49 49 55 55 55 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
result:
ok 90 lines
Test #7:
score: 1
Accepted
time: 3ms
memory: 47520kb
input:
50 2450 21 49 31 13 28 21 35 9 40 19 8 18 8 41 13 46 5 2 46 9 30 46 37 36 4 19 23 33 28 39 2 23 38 28 13 26 46 44 29 27 35 15 10 23 49 33 2 6 16 22 2 10 29 15 18 5 15 40 46 29 18 47 31 5 9 45 18 29 15 27 40 27 12 43 14 22 8 31 50 29 16 4 35 43 36 40 16 34 28 14 30 13 9 40 44 47 33 36 10 29 26 33 8 1...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 99 100 101 102 103 106 109 110 113 114 115 116 117 118 119 120 121 12...
result:
ok 2450 lines
Test #8:
score: 1
Accepted
time: 3ms
memory: 45808kb
input:
50 2450 18 43 47 16 11 47 49 15 38 20 5 6 22 9 45 25 19 31 47 41 4 12 48 13 48 43 14 44 24 36 28 31 34 2 11 8 11 22 37 30 44 1 3 8 34 29 42 47 18 5 27 8 37 34 2 45 1 24 3 31 40 2 41 46 1 47 13 18 48 49 28 27 3 46 1 4 27 21 16 42 26 5 37 4 17 50 36 8 42 29 7 14 29 5 23 32 40 29 48 11 42 28 30 18 37 1...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
result:
ok 2450 lines
Test #9:
score: 0
Wrong Answer
time: 3ms
memory: 45984kb
input:
50 2450 47 41 47 20 25 27 30 33 32 31 34 39 12 9 20 22 39 32 3 17 35 5 32 29 39 48 10 11 25 16 12 49 28 42 4 37 40 48 48 12 46 49 26 2 36 12 27 45 32 23 26 44 47 35 33 1 26 1 1 18 3 27 34 43 47 43 11 27 34 33 2 47 13 10 5 21 16 44 15 46 20 42 16 9 37 34 29 9 24 8 7 26 21 35 37 10 42 10 3 14 24 38 14...
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 103 104 105 106...
result:
wrong answer 166th lines differ - expected: '2307', found: '2167'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%