QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#251041 | #856. Cactus | thomas_li# | WA | 124ms | 3452kb | C++17 | 2.0kb | 2023-11-14 06:38:35 | 2023-11-14 06:38:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x,y) x = max(x,y)
#define cmn(x,y) x = min(x,y)
#define ary(k) array<int,k>
typedef pair<int,int> pii;
typedef vector<int> vi;
const int MOD = 1e9+7;
void solve(){
int n,m,k; cin >> n >> m >> k;
vector<vi> adj(n);
rep(i,0,m){
int u,v; cin >> u >> v;
u--; v--;
adj[u].PB(v); adj[v].PB(u);
}
vector<vi> dp(n+1,vi(2));
dp[1][0] = k; dp[1][1] = 0;
rep(i,2,n+1){
dp[i][0] = dp[i-1][1];
dp[i][1] = ((k-1)*dp[i-1][0]%MOD+(k-2)*dp[i-1][1]%MOD);
}
auto fpow = [&](int x, int y){
int z = 1; for(; y; y /= 2, x = x*x%MOD) if(y & 1) z = z*x%MOD;
return z;
};
int rat = (k-1)*fpow(k,MOD-2)%MOD;
vi vis(n),stk,cyc(n,-1),cnt(n); int id = 0;
auto dfs = [&](auto& dfs, int u, int p)->void{
vis[u] = 1; stk.PB(u);
for(int v : adj[u]) if(v != p){
if(!vis[v]) dfs(dfs,v,u);
else if(vis[v] == 1){
for(int i = sz(stk)-1; i >= 0; i--){
cyc[stk[i]] = id;
//cout << id << " " << stk[i] << "\n";
cnt[id]++;
if(stk[i] == v) break;
}
id++;
}
}
vis[u] = 2; stk.pop_back();
};
dfs(dfs,0,-1);
rep(i,0,n)if(cyc[i] == -1){
cnt[i] = 1;
cyc[i] = id++;
}
vector<vi> g(id);
rep(u,0,n) for(int v : adj[u]) if(cyc[u] != cyc[v]){
//cout << cyc[u] << " " << cyc[v] << "\n";
g[cyc[u]].PB(cyc[v]);
}
auto dfs1 = [&](auto& dfs1, int u, int p)->int{
int cur = (cnt[u] == 1 ? k : dp[cnt[u]][1]);
for(int v : g[u]) if(v != p){
int nxt = dfs1(dfs1,v,u);
cur = cur*nxt%MOD*rat%MOD;
}
//cout << u << " " << cur << "\n";
return cur;
};
int ans = dfs1(dfs1,0,-1);
cout << ans << "\n";
}
signed main(){
cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
int t; cin >> t; while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3388kb
input:
2 2 1 100 1 2 6 7 3 1 2 2 3 3 1 4 5 5 6 6 4 1 4
output:
9900 24
result:
ok 2 number(s): "9900 24"
Test #2:
score: -100
Wrong Answer
time: 124ms
memory: 3452kb
input:
50000 9 10 4 4 7 5 2 1 5 7 3 9 6 8 3 3 2 9 1 4 8 6 2 10 11 4 4 1 1 3 5 1 10 9 8 4 1 6 7 9 7 10 8 1 1 9 10 2 10 10 4 7 5 6 9 5 1 9 7 10 9 4 9 5 10 2 3 3 7 3 8 9 10 4 3 9 3 7 5 4 6 2 1 9 6 5 4 2 9 8 5 1 7 8 9 9 4 9 4 4 1 6 3 8 7 2 9 6 7 1 8 6 9 5 2 10 11 4 7 8 6 2 9 10 7 2 2 4 4 7 3 7 3 1 10 6 6 9 5 1...
output:
15120 0 0 0 0 0 19692 0 0 0 0 0 0 0 0 0 0 0 0 0 0 40824 0 0 0 13176 0 59040 0 0 0 13176 0 0 19680 599760 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19680 0 0 0 0 0 0 0 0 0 137180 26244 0 0 0 0 4191750 0 0 0 0 0 19692 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15120 0 0 0 0 0 19680 13176 0 0 0 0 0 0 19680 0 0 0 0 0 ...
result:
wrong answer 2nd numbers differ - expected: '34992', found: '0'