QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#523936 | #4433. Kitten and Roomba | sroid_03# | AC ✓ | 1654ms | 166708kb | C++14 | 3.3kb | 2024-08-18 23:59:44 | 2024-08-18 23:59:45 |
Judging History
answer
// by Siddhid Saha (2112010)
#include <bits/stdc++.h>
using namespace std;
#define INF 1e18
#define endl "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define PI atan(1)*4
#define set_bits __builtin_popcountllO
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define rvsort(a) sort(all(a),greater<int>())
#define read(a,n) for(int i = 0 ; i < n ; i ++){ cin >> a[i];}
#define printv(a) for(auto it: a){cout << it << " ";} cout << endl;
#define ms(arr, v) memset(arr, v, sizeof(arr))
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
#include "/Users/templates/debug.h"
#else
#define dbg(x...)
#endif
/*---------------------------------------------------------------------------------------------------------------------------*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
void google(int t) {cout << "Case #" << t << ": ";}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
ll uid(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);}
/*--------------------------------------------------------------------------------------------------------------------------*/
//const int mod = 1e9 + 7;
//const int mod = 998244353;
void solve()
{
ll n, st; cin >> n >> st;
vector<vll> adj(n+1);
for(int i = 0 ; i < n-1 ; i ++){
ll u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
lld ans = 0;
vll par(n+1);
auto dfs = [&](auto &&dfs, ll node, ll p)->void{
par[node] = p;
for(auto it: adj[node]){
if(it != p){
dfs(dfs, it, node);
}
}
};
dfs(dfs, 1, 0);
ll m; cin >> m;
vll mov(m); read(mov, m);
vector<lld> dp(n+1, 0), lazy(n+1, 0LL);
dp[st] = 1;
for(int i = 0 ; i < m ; i ++){
dp[mov[i]] += lazy[par[mov[i]]];
ans += dp[mov[i]];
ll sz = adj[mov[i]].size();
dp[par[mov[i]]] += dp[mov[i]]/sz;
lazy[mov[i]] += dp[mov[i]]/sz;
dp[mov[i]] = -lazy[par[mov[i]]];
dbg(ans);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t = 1;
cin >> t;
for(int i = 1 ; i <= t ; i++){
//google(i);
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1311ms
memory: 147272kb
input:
2 1000000 315562 969409 917725 324847 719085 524235 603427 576843 433171 75335 238378 266746 487233 80422 95099 594363 96140 858172 261406 958326 466109 233845 350950 863969 345645 689972 81395 395383 27274 93913 208983 523722 380358 108074 172341 130041 692304 737158 383812 752080 33646 154356 6672...
output:
5.60942 5.61051
result:
ok 2 numbers
Test #2:
score: 0
Accepted
time: 415ms
memory: 42596kb
input:
3 3 3 2 1 2 3 4000000 2 1 2 1 2 3 2 3 2 3 2 1 2 3 2 1 2 3 2 1 2 3 2 3 2 3 2 3 2 1 2 3 2 1 2 3 2 1 2 1 2 1 2 1 2 1 2 1 2 3 2 1 2 3 2 3 2 3 2 3 2 1 2 1 2 1 2 1 2 1 2 1 2 3 2 1 2 1 2 3 2 3 2 1 2 3 2 3 2 1 2 1 2 1 2 3 2 3 2 3 2 3 2 1 2 3 2 1 2 1 2 3 2 3 2 1 2 3 2 3 2 3 2 3 2 3 2 1 2 3 2 1 2 3 2 1 2 1 2 ...
output:
2.0005e+06 5e+06 666789
result:
ok 3 numbers
Test #3:
score: 0
Accepted
time: 515ms
memory: 4840kb
input:
5050 178 146 7 29 45 132 140 19 48 80 50 147 24 98 176 94 45 124 3 161 36 94 4 33 24 81 94 123 4 15 12 170 152 95 55 152 24 131 94 32 121 150 37 167 146 80 65 152 99 150 24 60 8 29 5 163 64 50 93 67 19 13 24 153 80 152 29 120 67 59 138 48 5 17 19 35 94 116 57 142 80 53 6 45 29 80 150 40 47 161 62 94...
output:
3.47452 2.125 23.955 0 2.5 3.93596 3.10684 5.58879 3.10662 1.79167 5.55553 0 4.86833 3.49933 0 3.10685 5.67287 5.68328 0 3.074 4.26906 3.10685 6.26347 12.4432 4.34153 3.10685 0 3.22638 3.62246 0 3.82936 3.02344 4.77796 5.11235 8 3.37341 5.64667 7 5.66454 2.675 5.20077 3.01855 3.29478 3.54754 4.04261...
result:
ok 5050 numbers
Test #4:
score: 0
Accepted
time: 1311ms
memory: 147796kb
input:
2 1000000 315562 679816 923554 749026 119526 400361 398944 729861 38631 237682 984276 240713 304346 923009 28429 705303 35145 281546 196216 128884 76719 542097 696978 832261 79936 617939 739512 639643 738806 304260 52873 63627 552308 627252 842013 683909 619035 326617 406438 159332 82575 823300 4115...
output:
4.45264 4.44664
result:
ok 2 numbers
Test #5:
score: 0
Accepted
time: 1280ms
memory: 147200kb
input:
2 1000000 315562 575035 646638 803204 719085 692015 374086 755314 193304 776395 55874 976706 805712 217914 823156 919201 953003 149774 16297 569437 50630 191605 6485 126613 286636 24494 152244 160536 434534 692015 362579 892731 521828 374895 872623 583943 946248 226984 256881 130161 826314 247361 45...
output:
5.83755 4.27158
result:
ok 2 numbers
Test #6:
score: 0
Accepted
time: 1654ms
memory: 166708kb
input:
2 1000000 941595 260117 135833 814046 740606 747365 965295 391550 71159 728551 704248 786875 854209 980320 968654 685130 721737 464879 19066 485673 803636 761076 467129 693561 787751 69739 373415 994214 367199 13100 494671 996272 547209 992937 103917 484331 476434 493297 779246 882922 78092 622726 3...
output:
8.81481 11.3321
result:
ok 2 numbers
Extra Test:
score: 0
Extra Test Passed