QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#387938 | #4885. Triangular Cactus Paths | Giga_Cronos# | WA | 99ms | 16068kb | C++23 | 2.6kb | 2024-04-13 05:44:51 | 2024-04-13 05:44:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fs first
#define sc second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
#define fl '\n'
int n,m;
vi g[200005];
vi t[200005];
bool mk[200005];
int low[200005],num[200005];
int cc=1;
int prnt[200005];
bool lt[200005];
void dfs(int u,int p){
mk[u]=true;
num[u]=cc;
low[u]=cc;cc++;
for(auto v:g[u]){
if(v==p)continue;
if(!mk[v]){dfs(v,u);t[u].pb(v);prnt[v]=u;}
low[u]=min(low[u],low[v]);
}
if(low[u]==num[p]){
lt[u]=1;
}
}
int pr[200005][25];
int lvl[200005];
int dp[200005];
int f[200005];
void build(int u){
if(lt[u])dp[u]++;
for(int i=1;i<=__lg(lvl[u]);i++){
pr[u][i]=pr[pr[u][i-1]][i-1];
}
for(auto v:t[u]){
pr[v][0]=u;
lvl[v]=lvl[u]+1;
dp[v]=dp[u];
build(v);
}
}
int kthp(int u,int k){
int l=__lg(k);
while(k){
u=pr[u][l];
k-=(1ll<<l);
l=__lg(k);
}
return u;
}
int lca(int a,int b){
if(lvl[a]<lvl[b])swap(a,b);
a=kthp(a,lvl[a]-lvl[b]);
if(a==b)return a;
for(int i=__lg(lvl[a]);i>=0;i--){
if(pr[a][i]!=pr[b][i]){
a=pr[a][i];
b=pr[b][i];
}
}
return pr[a][0];
}
int mod=998244353;
int qpow(int a,int b){
if(b==0)return 1;
if(b==1)return a;
int r=qpow(a,b/2);
int ret=r*r%mod;
if(b%2==0){
return ret;
}else{
return ret*a%mod;
}
}
int inv(int a,int b){
return a*qpow(b,mod-2)%mod;
}
int C(int n,int k){
return inv(f[n],f[k]*f[n-k]%mod);
}
int sol2[200005];
void solve(){
cin>>n>>m;
f[0]=1;
for(int i=1;i<=n;i++){
f[i]=(f[i-1]*i)%mod;
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
dfs(1,-1);
lvl[1]=1;
build(1);
int q;
cin>>q;
for(int i=0;i<q;i++){
int a,b,k;
cin>>a>>b>>k;
if(a==b){
if(k==0)cout<<1<<fl;
else cout<<0<<fl;
continue;
}
if(lvl[a]>lvl[b])swap(a,b);
int lc=lca(a,b);
int rs=lvl[a]-lvl[lc]+lvl[b]-lvl[lc];
rs-=dp[a]-dp[lc];
rs-=dp[b]-dp[lc];
if( (a==lc ||b==lc) && a!=b && lt[lc] )rs--;
if(lt[a])rs++;
if(lt[b])rs++;
int ck=dp[a]-dp[lc]+dp[b]-dp[lc];
if( (a!=lc && low[kthp(a,lvl[a]-lvl[lc]-1)]==num[prnt[lc]]) || (b!=lc && low[kthp(b,lvl[b]-lvl[lc]-1)]==num[prnt[lc]]) ){
ck++;
}
if(rs<=k){
cout<<C(ck,k-rs)<<fl;
}else{
cout<<0<<fl;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 15924kb
input:
8 10 1 2 2 3 3 1 3 4 4 5 5 6 6 4 4 7 7 8 8 4 6 1 1 0 1 1 1 1 4 3 6 2 4 5 7 4 3 4 2
output:
1 0 1 2 1 0
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 14004kb
input:
2 1 1 2 8 1 1 0 1 1 1 1 2 0 1 2 1 2 1 0 2 1 1 2 2 0 2 2 1
output:
1 0 0 1 0 1 1 0
result:
ok 8 numbers
Test #3:
score: -100
Wrong Answer
time: 99ms
memory: 16068kb
input:
50 70 41 24 9 15 29 19 21 11 1 14 5 27 34 48 10 32 34 49 46 3 22 33 34 39 16 30 22 45 7 16 25 30 43 17 22 44 5 25 41 49 29 32 39 25 10 4 45 27 13 38 29 7 3 35 14 30 50 2 8 11 13 35 18 26 34 40 38 36 7 19 12 3 25 26 30 42 21 8 12 46 44 33 14 31 47 2 25 46 20 19 49 24 15 43 18 25 13 36 27 22 4 32 30 3...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 4 0 0 20 10 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 4 0 0 0 0 0 0 0 0 35 0 0 0 0 1 0 6 0 1 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
wrong answer 42nd numbers differ - expected: '15', found: '20'