QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#373776 | #5208. Jumbled Trees | InfinityNS | RE | 1ms | 4040kb | C++20 | 6.2kb | 2024-04-02 05:35:08 | 2024-04-02 05:35:08 |
Judging History
answer
#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
int mod=998244353;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}
const int maxn=1010;
struct edge{
int a,b,w;
};
vector<edge>e;
vector<int>vect[maxn];
vector<pii>scand;
void add_edge(int a,int b,int w){
vect[a].pb(e.size());
vect[b].pb(e.size());
e.pb({a,b,w});
}
int disc[maxn],mint[maxn],ctm,isbridge[maxn];
int get_nxt(int eid,int x){
if(e[eid].a==x)return e[eid].b;
return e[eid].a;
}
void find_bridges(int x,int p){
disc[x]=++ctm;
mint[x]=disc[x];
//printf("%d %d XX\n",x,disc[x]);
for(int i=0;i<vect[x].size();i++){
int id=vect[x][i];
if(id==p)continue;
int nxt=get_nxt(vect[x][i],x);
if(disc[nxt])mint[x]=min(mint[x],disc[nxt]);
else{
find_bridges(nxt,id);
mint[x]=min(mint[x],mint[nxt]);
}
//printf("%d %d | %d %d AAA\n",x,e[id].b,mint[e[id].b],disc[x]);
if(mint[nxt]>disc[x]){
isbridge[id]=1;
scand.pb({e[id].w,1});
}
}
}
int ncol[maxn],ecol[maxn],ncnt;
int compsum[maxn];
int s;
int n,m;
void go_color(int x,int p,int &sz){
ncol[x]=ncnt;
sz++;
for(int i=0;i<vect[x].size();i++){
int id=vect[x][i];
if(isbridge[id])continue;
ecol[id]=ncnt;
compsum[ncnt]=add(compsum[ncnt],e[id].w);
int nxt=get_nxt(id,x);
if(ncol[nxt])continue;
go_color(nxt,id,sz);
}
}
void color_comps(){
for(int i=1;i<=n;i++){
if(ncol[i])continue;
ncnt++;
int sz=0;
go_color(i,-1,sz);
//printf("%d SZ\n",sz);
scand.pb({ mul(compsum[ncnt],invv(2)),(sz-1)%mod});
}
}
bool calc_s(){
int ep=0;
s=0;
for(int i=0;i<scand.size();i++){
int v=scand[i].ff;
int cc=scand[i].ss;
// printf("%d %d \n",v,cc);
if(cc==0){
if(v!=0)return false;
}
else{
int pom=mul(v,invv(cc));
if(ep==0){
ep=1;
s=pom;
}
else{
if(s!=pom)return false;
}
}
}
return true;
}
vector<pair<int,vector<int>>>ops;
void apply_mst(int v,vector<int>o){
assert(o.size()==n-1);
ops.pb({v,o});
for(int i=0;i<o.size();i++){
int id=o[i];
e[id].w=sub(e[id].w,v);
//printf("%d ",id);
}
/*printf(" | %d APPLIED\n",v);
for(int i=0;i<e.size();i++){
printf("%d ",e[i].w);
}
printf(" STATE\n");*/
}
int pos[maxn];
void dfs(int x,int p,vector<int>&es){
pos[x]=1;
for(int i=0;i<vect[x].size();i++){
int id=get_nxt(vect[x][i],x);
if(pos[id])continue;
es.pb(vect[x][i]);
dfs(id,vect[x][i],es);
}
}
void apply_rand_mst(){
vector<int>edges;
memset(pos,0,sizeof(pos));
dfs(1,-1,edges);
apply_mst(s,edges);
}
int dfs2(int x,int p,vector<int>&es){
//printf("%d XX\n",x);
if(pos[x]==2){
pos[x]=1;
return 1;
}
pos[x]=1;
for(int i=0;i<vect[x].size();i++){
int id=vect[x][i];
int v=get_nxt(vect[x][i],x);
if(pos[v]==1)continue;
if(pos[v]==2){
es.pb(id);
return 1;
}
if(dfs2(v,id,es)){
es.pb(id);
return 1;
}
}
return 0;
}
vector<int>nadji_ciklus(int e1,int e2){
memset(pos,0,sizeof(pos));
pos[e[e2].a]=2;
pos[e[e2].b]=2;
vector<int>ret;
dfs2(e[e1].a,-1,ret);
//printf("%d ADA\n",ret.size());
memset(pos,0,sizeof(pos));
pos[e[e2].a]=2;
pos[e[e2].b]=2;
for(int i=0;i<ret.size();i++)pos[e[ret[i]].a]=pos[e[ret[i]].b]=1;
pos[e[e1].a]=1;
dfs2(e[e1].b,-1,ret);
//printf("%d ADA\n",ret.size());
ret.pb(e1);
ret.pb(e2);
return ret;
}
void solve_comp(int c){
vector<int>cand;
for(int i=0;i<e.size();i++){
if(ecol[i]==c)cand.pb(i);
}
while(cand.size()){
int id=cand.back();
cand.pop_back();
if(e[id].w==0)continue;
int id2=cand.back();
//printf("%d %d AA\n",id,id2);
vector<int>es=nadji_ciklus(id,id2);
/*for(int i=0;i<es.size();i++)printf("%d ",es[i]);
printf(" CIKLUS\n");*/
memset(pos,0,sizeof(pos));
for(int j=0;j<es.size();j++){
int id=es[j];
pos[e[id].b]=1;
pos[e[id].a]=1;
}
vector<int>qry,qry2;
for(int j=0;j<es.size();j++){
int id=es[j];
dfs(e[id].a,-1,qry);
}
qry2=qry;
for(int j=0;j<es.size();j++){
int idc=es[j];
if(idc==id)qry.pb(idc);
else if(idc==id2)qry2.pb(idc);
else{
qry.pb(idc);
qry2.pb(idc);
}
}
int goal=e[id].w;
apply_mst(goal,qry);
apply_mst(sub(0,goal),qry2);
}
}
int main(){
///freopen("test.txt","r",stdin);
scanf("%d %d %d",&n,&m,&mod);
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add_edge(u,v,w);
}
find_bridges(1,-1);
color_comps();
if(!calc_s()){
printf("-1\n");
return 0;
}
///printf("%d SS\n",s);
apply_rand_mst();
for(int i=1;i<=ncnt;i++){
solve_comp(i);
}
printf("%d\n",ops.size());
for(int i=0;i<ops.size();i++){
printf("%d ",ops[i].ff);
vector<int>&pom=ops[i].ss;
for(auto x:pom)printf("%d ",x+1);
printf("\n");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3852kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
5 60 1 2 50 1 3 51 1 2 30 3 2 71 3 1
result:
ok Participant found an answer (5 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
2 2 37 1 2 8 1 2 15
output:
3 23 1 15 2 22 1
result:
ok Participant found an answer (3 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 0ms
memory: 4032kb
input:
5 4 5 1 3 1 2 3 2 2 5 3 4 1 4
output:
-1
result:
ok Both jury and participant did not find an answer
Test #4:
score: 0
Accepted
time: 0ms
memory: 4040kb
input:
10 15 997 4 3 459 9 7 94 9 8 767 10 2 877 5 8 258 3 4 166 8 5 621 8 10 619 9 1 316 10 5 516 3 10 125 1 7 961 3 6 500 4 10 976 3 4 842
output:
-1
result:
ok Both jury and participant did not find an answer
Test #5:
score: -100
Runtime Error
input:
20 30 9973 1 10 696 3 8 2905 12 7 6609 20 10 1962 11 9 8430 19 2 412 6 3 6936 19 7 9113 14 15 5635 15 7 1770 13 10 3182 3 16 2625 17 1 7387 11 5 3700 9 15 1048 2 3 7717 12 10 8625 7 13 8141 5 14 2245 6 4 2819 18 19 8709 18 5 6191 17 10 7606 9 20 8626 17 4 8848 4 13 1073 10 8 2277 14 2 7714 11 8 5318...