QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#519391 | #8078. Spanning Tree | Timeless123 | WA | 36ms | 41252kb | C++17 | 4.0kb | 2024-08-14 19:35:15 | 2024-08-14 19:35:15 |
Judging History
answer
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
//#define int long long
#define RG register
#define bit(x) (1LL << (x))
#define lowbit(x) (x & -x)
#define sq(x) ((x) * (x))
#define rep(a, b, c, d) for (int a = (b); a <= (c); a += (d))
#define fep(a, b, c, d) for (int a = (b); a >= (c); a -= (d))
#define PLL pair<long, long>
using unll = unsigned long long;
using ll = long long;
/*--fast read--*/
// use : n = read<int>();
template<typename T> T read() {
T X = 0; bool flag = true; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') flag = false; ch = getchar(); }
while (ch >= '0' && ch <= '9') { X = (X << 1) + (X << 3) + ch - '0'; ch = getchar(); }
if (flag) return X;
return ~(X - 1);
}
// 毒瘤 只能Linux上用 win上报错
//template<typename T> T read() {
// T X = 0; bool flag = true; char ch = getchar_unlocked(); // 比getchar还快
// while (ch < '0' || ch > '9') { if (ch == '-') flag = false; ch = getchar_unlocked(); }
// while (ch >= '0' && ch <= '9') { X = (X << 1) + (X << 3) + ch - '0'; ch = getchar_unlocked(); }
// if (flag) return X;
// return ~(X - 1);
//}
// use : write<ll>(n);
template<typename T> void write(T X) {
if (X < 0) { putchar('-'); X = ~(X - 1); }
int s[100], top = 0;
while (X) { s[++top] = X % 10; X /= 10; }
if (!top) s[++top] = 0;
while (top) putchar(s[top--] + '0');
}
/*--const--*/
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MODE = 998244353;
const int dx[] = { 1, 0,-1, 0, 1, 1,-1,-1 };
const int dy[] = { 0,-1, 0, 1, -1, 1,-1, 1 };
const double eps = 1e-8;
double Pi = acos(-1.0);
/*--math--*/
long long qpow(long long x, long long y) {
x %= MODE;
long long res = 1;
while (y) {
if (y & 1) res = res * x % MODE;
x = x * x % MODE;
y >>= 1;
}
return res;
}
long long gcd(long long a, long long b) { // 最大公约数
while (b ^= a ^= b ^= a %= b)
;
return a;
}
long long lcm(long long a, long long b) { // 最小公倍数
return a / gcd(a, b) * b;
}
/*------------CODE------------*/
// int months[15] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//priority_queue<ll> pq;//这是一个大根堆q
//priority_queue<int, vector<int>, greater<int> >q;//这是一个小根堆q
//priority_queue<ll, vector<ll>, greater<ll> >pq; // 小根
const long long N = 1e6 + 50;
const long long M = 2e6 + 50;
ll n, m;
//[表情]ector<ll>adj[N];
vector<pair<ll, ll> >vp;
vector<ll>cst[N];
ll fa[N];
ll sz[N];
ll d[N];
ll find(ll x)
{
return (x == fa[x] ? x : (fa[x] = find(fa[x])));
}
ll p[N];
void dfs(ll x, ll fa)
{
for (auto& y : cst[x])
if (y != fa) {
p[y] = x, dfs(y, x);
d[y] = d[x] + 1;
}
}
void solve()
{
cin >> n;
for(int i = 1; i <= n;++ i)
{
fa[i] = i;
sz[i] = 1;
}
ll u, v;
vp.push_back({0,0});
for(int i = 1; i < n;++ i)
{
cin >> u >> v;
vp.push_back({ u, v });
}
for(int i = 1; i < n;++ i)
{
cin >> u >> v;
cst[u].push_back(v);
cst[v].push_back(u);
}
p[1] = 1;
dfs(1, 0);
//for(int i = 1;i <= n;++i) cout<<p[i]<<' '; cout<<'\n';
ll ans = 1;
for(int i = 1; i < n;++ i)
{
u = vp[i].first;
v = vp[i].second;
ll fu = find(u);
ll fv = find(v);
// cout<< fu << ' ' << fv <<'\n';
if (!(find(p[fu]) == fv || find(p[fv]) == fu))
{
cout << "0\n";
return;
}
ans = (((ans * sz[fu]) % MODE) * sz[fv]) % MODE;
fu = find(fu) , fv = find(fv);
if(fu == fv) continue;
if (d[fu] > d[fv]) swap(fu, fv);
fa[fv] = fu;
sz[fu] += sz[fv];
}
//cout << ans << '\n';
cout << qpow(ans, MODE - 2) % MODE << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("wrt.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T = 1;
//cin >> T;
while (T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 32628kb
input:
3 1 2 1 3 1 2 1 3
output:
499122177
result:
ok single line: '499122177'
Test #2:
score: -100
Wrong Answer
time: 36ms
memory: 41252kb
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'