QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789188 | #6127. Kawa Exam | forest114514 | WA | 456ms | 49688kb | C++20 | 4.8kb | 2024-11-27 19:29:28 | 2024-11-27 19:29:29 |
Judging History
answer
//蒟蒻一枚 rp++
//即得易见平凡,仿照上例显然。留作习题答案略,读者自证不难
//反之亦然同理,推论自然成立,略去过程Q.E.D.,由上可知证毕
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define re register
#define il inline
#define gc() getchar()
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define repp(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define tep(i,x) for(int i=head[x];~i;i=ne[i])
#define ls(x) x<<1
#define rs(x) x<<1|1
#define eps (1e-9)
#define inf 0x3f3f3f3f
#define INF 1e16
#define pii pair<int,int>
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define fi first
#define sc second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef double db;
namespace IO{
// #define gc()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<9,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<9],*p1=buf,*p2=buf;
template<typename T> inline void read(T &x){
bool f=1;x=0;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=gc();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch&15),ch=gc();
x=f?x:-x;
}
template<typename T> inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(char('0'+x%10));
}
template <typename T,typename ...Args> inline
void read(T &x,Args &...args){read(x);read(args...);}
template<typename T> inline void write(T x,char c){write(x),putchar(c);}
}
using namespace IO;
namespace MOD{
typedef uint32_t u32;
typedef uint64_t u64;
typedef __uint128_t u128;
u64 p,m;
void set_mod(u32 mod){
m=mod,p=-m/m+1;
}
u64 mo(u64 x){//取模优化
x-=u64((u128)x*p>>64)*m;
return (x>=m)?x-m:x;
}
}
bool _ST;
const int N=5e5+100;
int n,m,a[N],ans[N],val[N];
int Ti,st[N],top,dfn[N],siz[N],son[N],low[N],col[N],cnt_dcc;
vector<pii> E[N],e[N];
vector<int> cc[N];
int num[N*2],cnt[N],mx,is[N];
bool vis[N];
void add(int x){
--cnt[num[x]++];
++cnt[num[x]];
mx=max(mx,num[x]);
}
void del(int x){
--cnt[num[x]--];
++cnt[num[x]];
while(!cnt[mx]) --mx;
}
void tarjan(int x,int ine){
low[x]=dfn[x]=++Ti,st[++top]=x;
add(a[x]);
for(auto i:e[x]){
int y=i.fi,id=i.sc;
if(!dfn[y]){
tarjan(y,id);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x]){
++cnt_dcc;
int u;
do{
u=st[top--];
col[u]=cnt_dcc;
}while(u!=y);
}
}
else if(id!=ine) low[x]=min(low[x],dfn[y]);
}
}
void dfs1(int x){
del(a[x]);
vis[x]=1;
for(auto i:e[x]) if(!vis[i.fi]) dfs1(i.fi);
}
void dfs2(int x,int ff){
siz[x]=cc[x].size(),son[x]=0,vis[x]=1;
for(auto i:E[x]) if(i.fi!=ff){
dfs2(i.fi,x);
siz[x]+=siz[i.fi];
if(siz[i.fi]>siz[son[x]]) son[x]=i.fi;
}
}
int SON;
void calc(int x,int ff,int op){
if(op) for(auto c:cc[x]) add(a[c]);
else for(auto c:cc[x]) del(a[c]);
for(auto i:E[x]) if(i.fi!=ff&&i.fi!=SON) calc(i.fi,x,op);
}
void dfs3(int x,int ff){
for(auto i:E[x]) {
if(i.fi==son[x]) is[x]=i.sc;
if(i.fi!=ff&&i.fi!=son[x]) {
dfs3(i.fi,x),ans[i.sc]+=mx;
calc(i.fi,x,0);
}
}
if(son[x]){
dfs3(son[x],x),ans[is[x]]+=mx;
SON=son[x];
}
calc(x,ff,1),SON=0;
}
void dfs4(int x,int ff){//子树补信息
if(son[x])SON=son[x];
calc(x,ff,1),SON=0;
if(!son[x]) return;
ans[is[x]]+=mx;
dfs4(son[x],x);
for(auto i:E[x]) if(i.fi!=ff&&i.fi!=son[x]){
calc(i.fi,x,0),ans[i.sc]+=mx;
dfs4(i.fi,x);
calc(i.fi,x,1);
}
}
void solve(){
read(n,m);Ti=cnt_dcc=0,cnt[0]=n;
rep(i,1,n)read(a[i]);
rep(i,1,n)dfn[i]=low[i]=col[i]=ans[i]=val[i]=vis[i]=is[i]=0;
rep(i,1,m){
int u,v;read(u,v);
e[u].pb({v,i}),e[v].pb({u,i});
}
int al=0;
rep(i,1,n){
if(!dfn[i]){
int las=cnt_dcc;
tarjan(i,0);
if(top){
++cnt_dcc;
while(top) col[st[top--]]=cnt_dcc;
}
al+=mx;
rep(j,las,cnt_dcc) val[j]=mx;
dfs1(i);
}
}
rep(x,1,n){
cc[col[x]].pb(x);
for(auto i:e[x]){
int y=i.fi,id=i.sc;
if(col[x]==col[y]) ans[id]=al;
else{
E[col[x]].pb({col[y],id});
ans[id]=al-val[col[x]];
}
}
e[x].clear(),vis[x]=0;
}
rep(i,1,cnt_dcc){
if(!vis[i]){
dfs2(i,0);
dfs3(i,0),calc(i,0,0);
dfs4(i,0),calc(i,0,0);
}
}
rep(i,1,cnt_dcc) E[i].clear(),vis[i]=0,cc[i].clear();
rep(i,1,m) write(ans[i]),putchar((i<m)?' ':'\n');
}
bool _ED;
signed main(){
fprintf(stderr,"%.20lf MB\n",(&_ST-&_ED)/1048576.0);
//ios::sync_with_stdio(false);
//cin.tie(0);cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int T=1;read(T);
while(T--) solve();
fprintf(stderr,"%.4lf s\n",1.0*clock()/CLOCKS_PER_SEC);
return 0;
}
//谨记:
//十年OI一场空,不开longlong见祖宗
//数据千万条,清空第一条。多测不清空,爆零两行泪。清空不规范,TLE总相伴。
//快读要加类型名
/*
1
3 3
1 2 3
1 2
1 3
2 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 20356kb
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: -100
Wrong Answer
time: 456ms
memory: 49688kb
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 10 10 9 9 9 10 10 9 9 9 9 9 9 9 9 1...
result:
wrong answer 12th lines differ - expected: '10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11', found: '10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 10'