QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#424493 | #5208. Jumbled Trees | zyz07 | RE | 0ms | 3808kb | C++17 | 2.6kb | 2024-05-29 11:19:36 | 2024-05-29 11:19:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
ll power(ll x,ll y,ll mod){
ll r=1;
for(;y;y>>=1,x=x*x%mod){
if(y&1) r=r*x%mod;
}
return r;
}
const int N=505,M=1005;
int n,m,p,fa[N],fai[N],dep[N],vis[N],in_tree[M],vis2[M];
struct Edge{
int u,v,x;
}edge[M];
vector<pair<int,int>> g[N],e[N],g2[N];
void dfs(int u){
for(auto [v,i]:g[u]){
if(!vis[v]){
vis[v]=1;
fa[v]=u;
fai[v]=i;
dep[v]=dep[u]+1;
e[u].emplace_back(v,i);
e[v].emplace_back(u,i);
in_tree[i]=1;
dfs(v);
}
}
}
struct DSU{
int fa[M];
int find(int u){
return u==fa[u]?u:fa[u]=find(fa[u]);
}
}dsu;
pair<int,int> exc[M];
int ans[M];
void get_sum(int u,int fa,int& sum,int& cnt){
(sum+=edge[u].x)%=p;
cnt+=in_tree[u];
for(auto [v,i]:g2[u]){
if(v!=fa){
get_sum(v,u,sum,cnt);
}
}
}
void dfs2(int u,int fa,int fai){
vis2[u]=1;
for(auto [v,i]:g2[u]){
if(v!=fa){
dfs2(v,u,i);
}
}
if(edge[u].x){
assert(fa);
auto [x,y]=exc[fai];
if(u==x){
(ans[1]+=edge[u].x)%=p;
(ans[fai]+=-edge[u].x+p)%=p;
(edge[fa].x+=edge[u].x)%=p;
edge[u].x=0;
}else{
(ans[1]+=-edge[u].x+p)%=p;
(ans[fai]+=edge[u].x)%=p;
(edge[fa].x+=edge[u].x)%=p;
edge[u].x=0;
}
}
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m>>p;
For(i,1,m){
cin>>edge[i].u>>edge[i].v>>edge[i].x;
g[edge[i].u].emplace_back(edge[i].v,i);
g[edge[i].v].emplace_back(edge[i].u,i);
}
dfs(1);
iota(dsu.fa+1,dsu.fa+m+1,1);
int tot=0;
exc[++tot]={0,0};
For(i,1,m){
if(!in_tree[i]){
auto& [u,v,x]=edge[i];
if(dep[u]<dep[v]){
swap(u,v);
}
for(int x=u;x!=v;x=fa[x]){
if(dsu.find(i)!=dsu.find(fai[x])){
exc[++tot]={fai[x],i};
g2[i].emplace_back(fai[x],tot);
g2[fai[x]].emplace_back(i,tot);
dsu.fa[dsu.find(i)]=dsu.find(fai[x]);
}
}
}
}
{
int sum=0,cnt=0;
get_sum(1,0,sum,cnt);
int x=1LL*sum*power(cnt,p-2,p)%p;
ans[1]=x;
For(i,1,m){
if(in_tree[i]){
(edge[i].x+=-x+p)%=p;
}
}
}
For(i,1,m){
int sum=0,cnt=0;
get_sum(i,0,sum,cnt);
if(sum){
cout<<"-1\n";
return 0;
}
}
For(i,1,m){
if(vis2[i]) continue;
dfs2(i,0,0);
}
cout<<tot<<'\n';
For(i,1,tot){
cout<<ans[i]<<' ';
For(j,1,m){
if(in_tree[j]&&j!=exc[i].first){
cout<<j<<' ';
}
}
if(exc[i].second){
cout<<exc[i].second;
}
cout<<'\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3772kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
3 20 1 3 10 1 2 30 3 2
result:
ok Participant found an answer (3 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3708kb
input:
2 2 37 1 2 8 1 2 15
output:
2 8 1 15 2
result:
ok Participant found an answer (2 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 0ms
memory: 3728kb
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: 3728kb
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: 0
Accepted
time: 0ms
memory: 3692kb
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...
output:
30 1016 1 2 3 5 6 7 8 9 13 15 18 19 20 21 22 24 25 29 30 749 1 2 3 5 6 7 8 9 13 15 18 19 20 21 22 25 29 30 4 326 1 2 3 6 7 8 9 13 15 18 19 20 21 22 24 25 29 30 4 4057 1 2 3 5 6 7 8 9 13 15 18 19 20 21 22 24 25 30 4 4193 1 3 5 6 7 8 9 13 15 18 19 20 21 22 24 25 29 30 4 2439 1 2 3 5 6 8 9 13 15 18 19...
result:
ok Participant found an answer (30 trees) and jury found an answer (59 trees)
Test #6:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
50 80 99991 6 5 67664 39 4 74944 11 9 13035 13 48 81979 40 20 57943 20 31 72081 1 6 39307 48 39 3550 28 48 41071 18 28 42935 37 32 7538 37 29 3815 50 37 88043 38 41 7283 40 26 66278 37 34 60696 47 19 80875 4 26 67 20 32 91858 39 24 83485 45 25 12241 48 46 61691 37 44 47541 39 40 70034 37 42 25006 27...
output:
-1
result:
ok Both jury and participant did not find an answer
Test #7:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
100 150 999983 84 10 999545 69 48 930138 48 13 303468 36 6 668122 91 84 115623 62 71 59711 12 37 749281 86 49 281976 26 46 624831 91 8 450475 92 55 460900 50 63 513056 72 2 477622 26 96 11359 31 82 953946 6 71 406339 24 7 177090 70 4 67359 31 39 795565 47 32 407459 26 35 760698 22 37 508175 8 93 612...
output:
-1
result:
ok Both jury and participant did not find an answer
Test #8:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
200 250 9999991 170 185 3242943 70 17 6083198 137 55 4000889 15 171 1113989 108 65 7988488 192 37 8812990 53 143 8707264 80 180 2504807 55 163 2706048 67 64 6210980 87 165 7693967 155 122 8550804 56 99 7228534 114 138 7047731 190 196 6684929 86 197 8866886 38 195 6717874 112 133 7257617 160 104 3210...
output:
-1
result:
ok Both jury and participant did not find an answer
Test #9:
score: -100
Runtime Error
input:
500 600 99999989 265 416 47066772 354 266 16969437 195 415 7917612 354 136 43128175 163 191 58723996 144 84 65835385 157 45 94124747 232 441 17509499 70 397 64101208 223 387 7043647 320 47 84970673 100 2 87310855 87 131 75042257 101 391 27645446 79 26 68547739 390 185 92142961 257 15 80922292 276 48...