QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#589287#8078. Spanning TreeSwd146296#WA 178ms97352kbC++202.1kb2024-09-25 17:02:212024-09-25 17:02:21

Judging History

你现在查看的是最新测评结果

  • [2024-09-25 17:02:21]
  • 评测
  • 测评结果:WA
  • 用时:178ms
  • 内存:97352kb
  • [2024-09-25 17:02:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int MX=1000010;
const int p=998244353;

int N;
int ar[MX], br[MX], cr[MX], dr[MX];
int fa[MX], siz[MX];
map<int,int> blo[MX];
vector<int> v[MX];
int read()
{
    int r=0, f=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
    return r*f;
}
ll qmul(ll a, ll b)
{
    ll tmp=a*b-(int)((ld)a/p*b+0.5)*p; tmp%=p;
    return tmp<ll(0)?tmp+p:tmp;
}
ll qpow(ll x, ll pow)
{
    ll ans=1;
    while(pow)
    {
        if(pow&1) ans=ans*x%p;
        x=x*x%p;
        pow>>=1;
    }
    return ans;
}
ll inv(ll x){ return qpow(x,p-2);}
int find(int x){return x==fa[x]?fa[x]:fa[x]=find(fa[x]);}
void merge(int x, int y) 
{
    x=find(x); y=find(y);
    if(siz[x]<siz[y]) swap(x,y);
    fa[y]=x; siz[x]+=siz[y];
    for(auto u:v[y]) 
        v[x].emplace_back(u);
    for(auto [id,num]:blo[y])
    {
        // cout << "   id=" << id << ", num=" << num << '\n';
        blo[x][id]+=num;
    }
        
}
int main()
{
    // freopen("testdata.in","r",stdin);
    N=read();
    for(int i=1;i<=N;i++) fa[i]=i, siz[i]=1, v[i].emplace_back(i);
    for(int i=1;i<N;i++)
    {   
        ar[i]=read(), br[i]=read();
    }
    for(int i=1;i<N;i++)
    {
        cr[i]=read(), dr[i]=read();
        blo[cr[i]][dr[i]]++;
        blo[dr[i]][cr[i]]++;
    }
    ll ans=1;
    for(int i=1;i<N;i++)
    {
        ar[i]=find(ar[i]);
        br[i]=find(br[i]);
        ll bas=inv(siz[ar[i]]*siz[br[i]]);
        if(siz[ar[i]]>siz[br[i]]) swap(ar[i],br[i]);
        ll num=0;
        for(auto u:v[ar[i]])
        {
            num+=blo[br[i]][u];
            // cout << "   u=" << u << ", num=" << num << '\n';
        }
        // cout << "bas=" << siz[ar[i]]*siz[br[i]] << ", fac=" << num << '\n';        
        merge(ar[i],br[i]);
        ans=qmul(qmul(ans,bas),num);
    }
    cout << ans << '\n';
    return (0-0);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 63024kb

input:

3
1 2
1 3
1 2
1 3

output:

499122177

result:

ok single line: '499122177'

Test #2:

score: -100
Wrong Answer
time: 178ms
memory: 97352kb

input:

100000
2183 5543
22424 59062
8387 24544
14668 74754
15788 58136
26846 99981
13646 57752
29585 62862
27383 65052
2013 13116
24490 91945
26006 61595
19000 78817
31371 67837
29311 82989
4298 20577
23212 77962
16510 85839
26010 98714
25314 96063
17206 82359
7478 76540
13890 99556
23277 64321
25684 92613...

output:

0

result:

wrong answer 1st lines differ - expected: '330864231', found: '0'