QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#709102 | #3556. Making Friends on Joitter is Fun | sitablechair | 0 | 3ms | 14128kb | C++14 | 2.9kb | 2024-11-04 11:33:13 | 2024-11-04 11:33:14 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define sz(x) (signed)x.size()
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,l,r) for(int i=r;i>=l;i--)
#define F "CHOLHH"
#define fio freopen(F".inp","r",stdin);freopen(F".out","w",stdout);
using namespace std;
#ifdef NCGM
#include<C:/NCGM/headers/debug.h>
#else
#define debug(...) "mmb"
#endif
const int N=1e5+3;
int n,m,f[N],f1[N],mmb[N];
ll ans=0;
map<int,int> mp[N],mp1[N];
vector<int> ahu,ahu1;
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;
ans-=1LL*f[a]*(f[a]+1);
ans+=1LL*f[a]*mmb[a];
ans-=1LL*f[b]*(f[b]+1);
ans+=1LL*f[b]*mmb[b];
int a1=find_set1(u),b1=find_set1(v);
if (sz(mp[a])<sz(mp[b])) swap(a,b);
if (sz(mp1[a1])<sz(mp1[b1])) swap(a1,b1);
ahu.clear();
ahu1.clear();
for(auto el: mp[b]) {
ahu.pb(el.ff);
if (mp1[el.ff].count(a) || mp1[el.ff].count(b)) ahu1.pb(el.ff);
}
for(auto el: mp1[find_set1(b)])
if (mp[el.ff].count(a1) || mp[el.ff].count(b1)) ahu1.pb(el.ff);
for(auto el: mp1[b1]) {
int tmp=mp[el.ff][b1];
mp[el.ff][a1]=mp[el.ff][b1];
mp[el.ff].erase(mp[el.ff].find(b1));
}
for(auto el: ahu) {
mp1[el][a]=mp1[el][b];
mp1[el].erase(mp1[el].find(b));
}
for(auto el: mp[b]) {
mp[a][el.ff]+=el.ss;
mmb[a]+=el.ss;
}
mp[b].clear();
for(auto el: mp1[b1]) mp1[a1][el.ff]+=el.ss;
mp1[b1].clear();
if (mp1[a1].find(a)!=mp1[a1].end()) mp1[a1].erase(mp1[a1].find(a));
if (mp1[a1].find(b)!=mp1[a1].end()) mp1[a1].erase(mp1[a1].find(b));
if (mp[a].find(b1)!=mp[a].end()) {
mmb[a]-=mp[a][b1];
mp[a].erase(mp[a].find(b1));
}
if (mp[a].find(a1)!=mp[a].end()) {
mmb[a]-=mp[a][a1];
mp[a].erase(mp[a].find(a1));
}
//if (u==1 && v==2) debug(mmb[a],find_set1(3));
f[a]+=f[b];
f1[a1]+=f1[b1];
f[b]=a;
f1[b1]=a1;
ans+=1LL*f[a]*(f[a]+1);
ans-=1LL*f[a]*mmb[a];
for(auto el: ahu1) {
if (find_set(el)==find_set(a)) continue;
unite(el,a);
}
}
void add_edge(int u,int v) {
if (find_set(u)==find_set(v) || mp[find_set(v)].count(find_set1(u))) return;
mmb[find_set(v)]++;
ans-=f[find_set(v)];
mp[find_set(v)][find_set1(u)]++;
mp1[find_set1(u)][find_set(v)]++;
if (mp[find_set(u)].count(find_set1(v))) unite(u,v);
}
/*
3 4
1 2
2 3
3 1
3 2
*/
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
For(i,1,n) f[i]=f1[i]=-1;
For(i,1,m) {
int u,v;
cin >> u >> v;
add_edge(u,v);
cout << ans << endl;
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 14128kb
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 25 25 25 25 25 25 25 25 25 25 25 25 25
result:
wrong answer 8th lines differ - expected: '12', found: '25'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%