QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#761230 | #9533. Classical Counting Problem | scallionsong | 25 | 261ms | 16956kb | C++14 | 4.3kb | 2024-11-18 21:35:14 | 2024-11-18 21:35:14 |
Judging History
answer
bool M1;
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define LL __int128
#define db double
#define LD long double
#define Pii pair<int,int>
#define Pll pair<ll,ll>
#define Pull pair<ull,ull>
#define Pdb pair<db,db>
#define fir first
#define sec second
#define vec vector<int>
#define pb push_back
#define qlr cerr<<"qlr\n"
#define dyh cerr<<"dyh\n"
#define pc(x) __builtin_popcount(x)
#define uni(x,y) uniform_int_distribution<int>(x,y)(rng)
#define unl(x,y) uniform_int_distribution<ll>(x,y)(rng)
#define unr(x,y) uniform_real_distribution<double>(x,y)(rng)
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define UF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define look_memory cerr<<'\n'<<abs(&M1-&M2)/1024.0/1024<<'\n'
#define look_time cerr<<'\n'<<clock()*1.0/CLOCKS_PER_SEC<<'\n'
mt19937 rng(time(0)^(*new int));
const int INF=0x3f3f3f3f;
const int Mod=998244353;
template<typename T>
inline void inc(T &a,T b){
if(b<0) b+=Mod;
a+=b;
if(a>=Mod) a-=Mod;
}
template<typename T>
inline void dec(T &a,T b){
if(b<0) b+=Mod;
a-=b;
if(a<0) a+=Mod;
}
template<typename T>
inline void muc(T &a,T b){
a=a*b%Mod;
}
template<typename T>
inline bool chkmin(T &a,T b){
if(a<=b) return false;
a=b;
return true;
}
template<typename T>
inline bool chkmax(T &a,T b){
if(a>=b) return false;
a=b;
return true;
}
int T,n;
ll ans;
ll w1[100010],w2[100010];
vec e[100010];
struct Dsu{
#define N 100000
int fa[N+10];
void init(int n){
F(i,1,n) fa[i]=i;
}
int fd(int x){
return fa[x]==x?x:fa[x]=fd(fa[x]);
}
void merge(int x,int y){
x=fd(x),y=fd(y);
fa[x]=y;
}
#undef N
}dsu;
struct Tree{
#define N 100000
int n,rt;
int fth[N+10],siz[N+10],son[N+10],top[N+10],dfn[N+10];
vec e[N+10];
void dfs1(int u,int fa){
fth[u]=fa;
siz[u]=1;
son[u]=0;
for(auto v:e[u]){
if(v==fa) continue;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int fa,int topf){
top[u]=topf;
dfn[u]=++dfn[0];
if(son[u]) dfs2(son[u],u,topf);
for(auto v:e[u]){
if(v==fa||v==son[u]) continue;
dfs2(v,u,v);
}
}
void build(){
dfs1(rt,0);
dfs2(rt,0,rt);
dfn[0]=0;
}
vec get_chain(int x){
vec res;
while(x){
res.pb(x);
x=son[x];
}
return res;
}
#undef N
}T1,T2;
void add(int x){
w1[T2.dfn[x]]=1;
while(x){
w2[T2.dfn[x]]++;
x=T2.fth[x];
}
}
void del(int x){
w1[T2.dfn[x]]=0;
while(x){
w2[T2.dfn[x]]--;
x=T2.fth[x];
}
}
void dfs1(int u,int fa){
add(u);
for(auto v:T1.e[u]){
if(v==fa) continue;
dfs1(v,u);
}
}
void dfs2(int u,int fa){
del(u);
for(auto v:T1.e[u]){
if(v==fa) continue;
dfs2(v,u);
}
}
int query(int x){
ll res=0;
while(x){
res+=x*w1[T2.dfn[x]]*w2[T2.dfn[x]];
x=T2.fth[x];
}
return res;
}
void solve(){
cin>>n;
F(i,1,n) e[i].clear();
F(i,1,n-1){
int x,y;
cin>>x>>y;
e[x].pb(y);
e[y].pb(x);
}
T1.n=n,T1.rt=1;
F(i,1,n) T1.e[i].clear();
dsu.init(n);
UF(i,n,1){
for(auto j:e[i]){
if(j<i) continue;
T1.e[i].pb(dsu.fd(j));
dsu.merge(j,i);
}
}
T2.n=n,T2.rt=n;
F(i,1,n) T2.e[i].clear();
dsu.init(n);
F(i,1,n){
for(auto j:e[i]){
if(j>i) continue;
T2.e[i].pb(dsu.fd(j));
dsu.merge(j,i);
}
}
T1.build();
T2.build();
ans=0;
F(i,1,n){
if(T1.top[i]!=i) continue;
vec V=T1.get_chain(i);
reverse(V.begin(),V.end());
for(auto j:V){
add(j);
for(auto k:T1.e[j]){
if(k!=T1.fth[j]&&k!=T1.son[j]) dfs1(k,j);
}
// cout<<j<<":\n";
// F(k,1,n) cout<<w1[T2.dfn[k]]<<' '<<w2[T2.dfn[i]]<<'\n';
ans+=j*query(j);
}
for(auto j:V){
del(j);
for(auto k:T1.e[j]){
if(k!=T1.fth[j]&&k!=T1.son[j]) dfs2(k,j);
}
}
}
cout<<ans<<'\n';
}
bool M2;
int main(){
// freopen("ex_a4.in","r",stdin);
// freopen("monotone.out","w",stdout);
srand(time(0)^(*new int));
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>T;
while(T--) solve();
look_memory;
look_time;
return 0;
}
/*
1
3
1 3
2 3
6
3
1 2
2 3
3
1 3
2 3
7
2 1
3 1
4 1
5 1
6 5
7 6
6
2 1
3 1
4 1
5 4
6 1
9
2 1
3 2
4 3
5 1
6 4
7 5
8 2
9 3
9
2 1
3 2
4 3
5 4
6 5
7 2
8 3
9 5
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 27ms
memory: 14200kb
input:
10000 10 10 2 5 2 7 2 1 7 4 10 6 2 3 7 9 7 8 5 10 6 5 1 6 9 5 4 6 3 1 7 5 2 4 8 4 10 5 10 7 6 1 6 4 1 3 4 5 1 9 1 8 7 10 6 2 8 10 8 7 5 7 9 7 3 5 4 3 2 4 6 8 1 7 10 4 10 10 3 2 3 9 2 1 2 4 2 6 4 5 4 7 4 8 4 10 6 1 3 6 7 6 5 6 9 7 2 5 4 1 10 9 8 6 10 3 5 6 5 1 6 10 3 4 1 7 1 8 1 9 3 2 10 10 9 10 3 9 ...
output:
1592 2672 1502 3164 1869 3983 1215 2628 1548 4137 957 2441 1865 2974 1530 1701 2261 2554 1760 2259 2323 3480 2319 1375 1648 4365 2377 1544 1912 1114 1697 2814 2208 2397 1360 1806 1747 2365 1418 1773 2343 2292 2475 1480 1650 1998 981 1378 1785 1984 3003 989 3453 1656 2008 2302 3492 2682 2393 2994 176...
result:
ok 10000 lines
Subtask #2:
score: 10
Accepted
Test #2:
score: 10
Accepted
time: 29ms
memory: 16168kb
input:
5000 20 20 19 7 20 16 19 2 7 15 16 13 2 11 7 6 2 14 2 12 15 5 14 17 2 4 20 3 6 18 20 10 5 1 16 9 14 8 15 20 7 17 15 7 10 17 13 7 18 10 9 17 6 18 16 18 14 16 1 10 19 17 3 18 2 13 8 19 5 1 11 14 20 8 4 18 12 11 20 20 1 17 1 18 20 3 1 9 18 16 18 10 18 12 18 6 9 5 16 19 18 4 5 11 17 14 18 15 17 7 6 13 7...
output:
20721 38034 34273 22829 17722 27544 46022 45137 16644 27269 28662 25035 26312 21148 14106 28176 17802 35960 12683 53453 11349 31418 12177 38063 13437 15209 40896 36164 24851 27149 33448 35621 21295 18051 15404 16388 23302 23641 22600 18168 15109 26323 22612 53786 26857 17201 29605 13181 18756 16472 ...
result:
ok 5000 lines
Test #3:
score: 10
Accepted
time: 25ms
memory: 14408kb
input:
5000 20 2 5 12 2 20 2 3 20 1 5 10 12 9 20 4 9 11 20 6 20 16 3 8 3 13 8 17 10 14 2 18 17 7 3 19 13 15 11 20 15 6 12 15 17 6 5 17 3 12 18 15 8 18 11 5 13 15 16 17 14 13 9 11 20 5 7 13 1 8 4 1 19 5 10 9 2 3 20 5 15 10 15 3 5 9 15 20 5 6 10 17 20 16 6 11 10 19 17 7 5 4 16 14 17 13 17 12 14 18 13 2 5 8 3...
output:
12838 26475 28287 25843 18772 22056 29113 38525 15746 14068 27472 30414 24262 17868 21848 45416 16243 11822 29882 14933 54527 29300 18796 15975 26275 65954 32332 32468 25904 26481 15872 14722 12571 16132 12703 29222 14381 14446 58713 50838 32213 20501 24381 18175 24763 29058 16690 18122 20539 32235 ...
result:
ok 5000 lines
Subtask #3:
score: 10
Accepted
Test #4:
score: 10
Accepted
time: 27ms
memory: 16436kb
input:
2500 40 29 30 24 29 36 29 7 24 18 36 4 18 21 24 17 21 1 17 14 30 19 24 34 19 12 34 11 30 23 30 16 34 38 14 35 24 27 29 20 34 3 17 22 17 31 21 26 14 5 14 2 20 15 16 39 4 37 38 10 3 25 22 33 29 32 10 13 35 9 18 8 2 6 23 40 34 28 7 40 32 10 33 32 2 32 7 10 22 7 21 10 31 22 40 32 37 32 25 22 16 22 39 10...
output:
810633 404452 117099 243743 296777 474314 650868 172328 309229 117742 243602 365391 470337 224328 611246 135238 282936 391899 241985 241809 365040 159975 153715 361771 436106 282863 365203 134808 384355 137564 271407 537918 241082 212081 412678 461768 430833 460584 236968 256887 457800 439758 244646...
result:
ok 2500 lines
Test #5:
score: 10
Accepted
time: 34ms
memory: 16212kb
input:
1666 60 48 60 14 48 35 48 10 48 28 10 43 60 57 60 12 60 38 12 29 28 22 57 17 22 9 60 16 48 21 43 8 57 52 43 37 8 5 28 27 17 24 5 34 29 49 57 3 27 23 37 53 16 13 12 47 37 6 27 39 43 56 13 36 47 19 9 25 19 2 60 32 9 41 25 7 23 45 23 31 5 44 7 46 8 11 16 42 14 26 13 30 24 18 26 55 8 58 28 54 46 1 26 15...
output:
721013 1066623 1062972 1881033 697159 1529292 773227 791222 1614211 2341895 651702 976430 2375602 741248 1733771 2261741 2252942 4137189 1426473 2388105 670178 896629 2879777 843521 1182424 2832567 4026284 2804238 1501560 508974 1290125 1013982 1845299 1256080 3065089 2015363 2166807 2717402 2216766...
result:
ok 1666 lines
Test #6:
score: 10
Accepted
time: 35ms
memory: 14312kb
input:
1250 80 56 47 60 56 34 56 24 60 19 47 18 34 69 24 64 69 20 64 39 34 42 20 28 60 50 64 33 47 67 24 51 47 62 33 5 34 8 24 31 24 61 5 22 19 79 22 12 5 71 28 77 18 70 67 26 64 75 67 16 60 7 42 49 64 11 42 76 51 73 5 35 49 15 33 65 8 78 47 23 62 44 19 38 22 45 39 37 73 57 12 53 37 36 50 43 51 14 24 21 22...
output:
9825885 3893226 6116875 10086749 5393096 6522315 5920355 7902829 3381069 7569894 11567605 8651236 8257852 8756874 4255356 5362908 2357220 1811999 2132744 7582576 6297893 3149082 6203806 8273964 3567508 3978460 2050230 3292482 4784268 3382210 6900055 4094135 11029041 10556808 5959680 5565869 14793452...
result:
ok 1250 lines
Test #7:
score: 10
Accepted
time: 38ms
memory: 14196kb
input:
1000 100 27 59 62 59 77 62 64 27 29 77 3 64 35 77 58 77 48 29 51 35 31 51 80 29 36 62 17 27 99 59 65 59 78 51 2 36 63 59 45 80 93 77 86 36 30 29 72 36 94 29 40 72 49 65 44 58 1 64 81 51 10 2 43 29 18 43 28 18 12 64 20 12 32 40 56 12 7 17 84 30 24 62 89 51 23 43 11 48 92 27 19 59 41 62 79 64 37 10 97...
output:
31974913 12093412 14105413 3172720 10694179 7923959 6328108 11225351 16726813 7154491 20683709 15448313 6702811 12057927 5646735 11944823 20882833 12781298 6563862 11034477 14478585 15412978 12307953 21275884 6567913 19896453 5099613 19928517 7874152 20318428 33070320 13452965 5982093 6647881 221816...
result:
ok 1000 lines
Subtask #4:
score: 0
Wrong Answer
Test #8:
score: 15
Accepted
time: 47ms
memory: 14564kb
input:
500 200 63 7 145 63 78 145 103 63 163 7 97 163 57 7 186 78 30 145 25 57 56 97 112 25 50 7 162 50 105 112 126 57 32 126 95 105 188 105 124 112 86 186 82 162 143 162 194 95 183 97 101 82 197 82 200 186 96 143 146 124 164 197 54 95 195 57 131 143 130 146 193 112 36 97 16 163 62 193 93 124 121 96 1 96 1...
output:
126443395 98757645 53031961 291855662 249009043 162378675 132960917 162056007 329056810 108103316 299902243 119131433 120999023 298936590 233906403 125093815 164591715 335168622 158851967 154337469 199778607 124138841 154231483 148367087 328821194 199730727 214600421 117839595 69955641 188267743 108...
result:
ok 500 lines
Test #9:
score: 0
Wrong Answer
time: 71ms
memory: 14388kb
input:
200 500 190 329 121 190 31 329 274 121 391 274 50 31 79 121 397 31 27 397 67 391 144 121 23 79 352 31 36 391 304 50 284 391 448 23 104 144 34 352 323 144 17 274 60 27 390 34 313 274 75 27 5 67 223 60 185 79 217 144 68 329 446 185 399 68 156 397 51 68 258 75 473 31 146 75 496 156 181 352 434 79 334 2...
output:
11987729284 7223244358 7398825793 21629508075 12391547999 1845716072 16824102868 1428371420 26942524822 14126836436 8028068381 11357167943 17691025134 6293222978 23358361715 14893037054 22909296501 25929428707 16925660262 10887833296 13154789503 16197063013 29637303521 6629129278 25699179594 1678848...
result:
wrong answer 1st lines differ - expected: '3397794692', found: '11987729284'
Subtask #5:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 261ms
memory: 16956kb
input:
33 3000 1322 1797 2442 1797 626 2442 1979 2442 485 1979 1428 1979 2196 1979 1499 1797 1936 2442 2921 1979 751 1499 2182 2921 979 1499 935 1979 1136 485 1880 1797 1084 2921 349 751 482 349 1009 979 1003 349 2056 482 2959 1136 1288 751 496 2442 1693 935 2045 1499 868 1009 1264 1428 2891 868 1045 1288 ...
output:
145648421595 133471540245 146419915650 211433049925 165703639015 90430453436 191324585476 100947301126 162568935028 118707596082 99373287779 165749364529 130564921368 156466318618 85792637490 157360350223 40496022991 247894456595 57542854696 257010613970 248878894298 223040304074 109294301210 250976...
result:
wrong answer 1st lines differ - expected: '3914500827', found: '145648421595'
Subtask #6:
score: 0
Time Limit Exceeded
Test #18:
score: 0
Time Limit Exceeded
input:
1 98303 65520 65521 65521 65519 65519 65522 65522 65517 65517 65518 65518 65516 65516 65523 65523 65513 65513 65514 65514 65512 65512 65515 65515 65510 65510 65511 65511 65509 65509 65524 65524 65505 65505 65506 65506 65504 65504 65507 65507 65502 65502 65503 65503 65501 65501 65508 65508 65498 6549...
output:
result:
Subtask #7:
score: 0
Time Limit Exceeded
Test #22:
score: 0
Time Limit Exceeded
input:
1 100000 16150 88283 9425 88283 87988 88283 52935 88283 69816 88283 51311 88283 6910 9425 59991 87988 54743 6910 19614 52935 22926 69816 91163 88283 17233 19614 64177 19614 92097 91163 57414 9425 96321 6910 17859 54743 59810 69816 66565 17859 9948 96321 84506 59810 3928 64177 63031 54743 6214 87988 ...