QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#93154 | #6127. Kawa Exam | lam | AC ✓ | 732ms | 40112kb | C++14 | 4.1kb | 2023-03-31 12:22:16 | 2023-03-31 12:22:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
#define ff first
#define ss second
const int maxn = 1e5 + 10;
int n,m,k;
int res;
int a[maxn],ans[maxn],par[maxn],heavy[maxn],s[maxn];
int in[maxn],out[maxn],inv[maxn],id[maxn];
bool vis[maxn];
ii e[maxn];
vector <ii> adj[maxn],adj2[maxn];
vector <int> b[maxn];
int l[maxn],t[maxn],cnt;
stack <int> st;
struct ds
{
int val[maxn],sl[maxn];
int ans = 0;
int _t;
void reset(int type)
{
_t=type;
fill_n(val,n+1,0);
fill_n(sl,n+1,0);
ans=0;
}
void add(int x)
{
sl[val[x]]--;
val[x]++;
sl[val[x]]++;
ans=max(ans,val[x]);
}
void del(int x)
{
sl[val[x]]--;
val[x]--;
sl[val[x]]++;
ans = (sl[ans]>0)?ans:(ans-1);
}
};
ds s1,s2;
void dfs(int x, int p)
{
st.push(x);
l[x]=t[x]=++cnt;
for (ii i:adj[x])
if (i.ss!=p)
{
if (t[i.ff]==0)
{
dfs(i.ff,i.ss);
l[x]=min(l[x],l[i.ff]);
}
else l[x]=min(l[x],t[i.ff]);
}
if (l[x]==t[x])
{
int temp; k++;
do
{
temp=st.top(); st.pop();
id[temp] = k;
b[k].push_back(a[temp]);
}
while (temp!=x);
}
}
void dfs2(int x, int p)
{
vis[x] = 1;
s[x] = b[x].size();
in[x] = ++cnt;
inv[cnt] = x;
int temp = -1;
for (ii i:adj2[x])
if (i.ff!=p)
{
par[i.ff] = i.ss;
dfs2(i.ff,x);
s[x] += s[i.ff];
if (temp==-1||s[i.ff]>s[temp]) temp = i.ff;
}
for (int i:b[x])
{
s2.add(i);
}
heavy[x] = temp;
out[x] = cnt;
}
void dfs3(bool keep, int x, int p)
{
for (ii i:adj2[x])
if (i.ff!=p&&i.ff!=heavy[x]) dfs3(0,i.ff,x);
if (heavy[x]!=-1) dfs3(1,heavy[x],x);
for (ii i:adj2[x])
if (i.ff!=p&&i.ff!=heavy[x])
for (int j=in[i.ff]; j<=out[i.ff]; j++)
for (int z:b[inv[j]])
{
s1.add(z);
s2.del(z);
}
for (int i:b[x])
{
s1.add(i);
s2.del(i);
}
if (par[x]!=-1)
{
ans[par[x]]+=s1.ans + s2.ans;
}
if (keep) return;
for (int i=in[x]; i<=out[x]; i++)
for (int j:b[inv[i]])
{
s1.del(j);
s2.add(j);
}
}
void xuly()
{
cin>>n>>m;
for (int i=1; i<=n; i++) b[i].clear();
fill_n(vis,n+1,0);
fill_n(t,n+1,0);
fill_n(l,n+1,0);
fill_n(par,n+1,-1);
fill_n(ans,m+1,0);
res=0;
for (int i=1; i<=n; i++)
{
adj[i].clear();
adj2[i].clear();
}
k=0; cnt=0;
vector <int> aa;
for (int i=1; i<=n; i++)
{
cin>>a[i];
aa.push_back(a[i]);
}
sort(aa.begin(),aa.end());
aa.resize(unique(aa.begin(),aa.end())-aa.begin());
for (int i=1; i<=n; i++)
{
a[i]=lower_bound(aa.begin(),aa.end(),a[i])-aa.begin()+1;
}
for (int i=1; i<=m; i++)
{
int u,v; cin>>u>>v;
adj[u].push_back({v,i});
adj[v].push_back({u,i});
e[i]={u,v};
}
for (int i=1; i<=n; i++) if (!t[i]) dfs(i,-1);
for (int i=1; i<=m; i++)
{
int u,v; u=e[i].ff; v=e[i].ss;
if (id[u]==id[v]) continue;
u=id[u]; v=id[v];
adj2[u].push_back({v,i});
adj2[v].push_back({u,i});
}
res=0;
cnt=0;
s1.reset(1); s2.reset(2);
n=k;
for (int i=1; i<=n; i++) if (!vis[i])
{
par[i] = -1;
dfs2(i,i);
int val = s2.ans;
dfs3(0,i,i);
for (int j=in[i]; j<=out[i]; j++)
for (int z:b[inv[j]]) s2.del(z);
for (int j=in[i]+1; j<=out[i]; j++)
ans[par[inv[j]]] -= val;
res+=val;
}
for (int i=1; i<=m; i++)
{
ans[i]+=res;
cout<<ans[i];
if (i!=m) cout<<' ';
}cout<<'\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tt; cin>>tt;
while (tt--) xuly();
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 15736kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 732ms
memory: 37360kb
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...
output:
2 2 2 2 2 2 2 6 6 7 6 6 6 6 6 3 3 3 4 4 3 3 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9 10 9 16 16 16 16 16 17 16 16 10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11 10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...
result:
ok 5557 lines
Test #3:
score: 0
Accepted
time: 433ms
memory: 37244kb
input:
10 100000 99999 3983 3983 20157 97983 20157 20157 3983 3983 97983 20157 20157 3983 97983 20157 3983 20157 20157 3983 3983 3983 97983 97983 20157 3983 3983 97983 20157 97983 20157 97983 3983 97983 97983 3983 20157 3983 20157 20157 97983 3983 3983 3983 3983 97983 97983 3983 97983 97983 3983 20157 3983...
output:
33392 33393 33393 33393 33393 33392 33392 33393 33393 33393 33392 33393 33393 33392 33393 33393 33392 33392 33392 33393 33393 33393 33392 33392 33393 33393 33393 33393 33393 33392 33393 33393 33392 33393 33392 33393 33393 33393 33392 33392 33392 33392 33393 33393 33392 33393 33393 33392 33393 33392 ...
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 449ms
memory: 40112kb
input:
10 100000 99999 27534 27534 3780 3780 27534 53544 27534 3780 3780 53544 53544 27534 53544 53544 3780 3780 3780 3780 53544 27534 3780 3780 53544 27534 27534 53544 27534 27534 53544 27534 27534 27534 3780 27534 27534 3780 3780 3780 27534 53544 3780 53544 27534 3780 3780 3780 27534 27534 27534 3780 275...
output:
33613 33601 33601 33600 33600 33601 33601 33601 33600 33601 33600 33600 33601 33601 33601 33601 33601 33601 33600 33600 33601 33601 33601 33601 33600 33601 33601 33600 33601 33600 33601 33600 33601 33601 33601 33601 33600 33601 33601 33601 33601 33601 33601 33601 33601 33601 33600 33601 33600 33601 ...
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 538ms
memory: 31744kb
input:
10 100000 99999 92499 92270 92270 92499 92499 92499 92270 54017 92270 92270 92270 54017 54017 54017 54017 92270 92499 54017 92270 54017 92499 92499 92270 92270 54017 54017 54017 54017 92270 92270 92499 54017 54017 92499 92499 54017 92270 92270 54017 92499 92270 92270 54017 54017 54017 92499 92499 54...
output:
33506 33482 33507 33482 33508 33483 33508 33483 33508 33483 33507 33483 33506 33483 33505 33483 33503 33483 33503 33482 33504 33483 33505 33483 33504 33483 33502 33483 33501 33483 33500 33482 33502 33483 33500 33483 33501 33482 33502 33483 33501 33483 33500 33482 33500 33483 33498 33483 33499 33483 ...
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 438ms
memory: 30320kb
input:
10 100000 99999 76207 76207 88551 88551 98176 76207 98176 88551 88551 98176 88551 76207 76207 98176 98176 76207 76207 88551 76207 88551 76207 88551 88551 76207 88551 76207 98176 88551 76207 98176 88551 88551 76207 88551 98176 88551 76207 76207 98176 88551 76207 98176 76207 88551 88551 88551 88551 76...
output:
33484 33484 33476 33484 33477 33485 33476 33485 33477 33485 33477 33486 33477 33484 33477 33485 33476 33485 33476 33485 33476 33483 33477 33483 33477 33485 33476 33485 33477 33485 33476 33487 33476 33487 33476 33486 33477 33486 33476 33486 33477 33486 33476 33486 33476 33486 33477 33487 33477 33487 ...
result:
ok 10 lines
Test #7:
score: 0
Accepted
time: 471ms
memory: 28852kb
input:
10 100000 99999 70486 49904 70486 49904 87935 49904 49904 87935 87935 49904 49904 87935 49904 87935 87935 70486 49904 87935 87935 49904 70486 87935 49904 70486 87935 87935 49904 49904 49904 87935 70486 70486 70486 49904 70486 87935 87935 87935 70486 87935 70486 49904 87935 49904 49904 87935 70486 87...
output:
33491 33486 33489 33486 33489 33486 33489 33486 33487 33486 33487 33486 33486 33485 33486 33486 33486 33486 33485 33486 33485 33485 33486 33486 33485 33486 33485 33486 33485 33485 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33486 33485 33485 33486 33485 ...
result:
ok 10 lines
Test #8:
score: 0
Accepted
time: 427ms
memory: 28820kb
input:
10 100000 99999 98004 33580 98004 98004 98004 92291 92291 98004 98004 92291 92291 33580 98004 92291 33580 98004 98004 33580 98004 92291 92291 33580 92291 92291 98004 33580 98004 33580 33580 98004 33580 92291 33580 33580 92291 92291 92291 98004 33580 98004 92291 92291 33580 92291 98004 98004 92291 92...
output:
33462 33463 33421 33463 33422 33465 33421 33463 33422 33464 33422 33462 33422 33464 33421 33464 33422 33464 33422 33465 33422 33463 33422 33462 33422 33463 33422 33465 33421 33464 33422 33464 33422 33463 33422 33463 33421 33463 33421 33462 33422 33460 33422 33461 33421 33461 33422 33460 33422 33459 ...
result:
ok 10 lines