Submission #2834662
Source Code Expand
#include<bits/stdc++.h> #define ll long long using namespace std; #define pb push_back const int ha=1e9+7,N=200005; inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;} inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;} inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an; } inline int read(){ int x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return x; } vector<int> g[N],now; int dfn[N],dc,num[N],jc[N],ni[N],ans=1,sz; int n,X,Y,col[N],w[N],mn[N],p[N],pos[2],C; bool v[N]; inline void ae(int x,int y){ g[x].pb(y),g[y].pb(x);} inline void init(){ // prepare n! and (n!)^(-1) jc[0]=1; for(int i=1;i<=n;i++) jc[i]=jc[i-1]*(ll)i%ha; ni[n]=ksm(jc[n],ha-2); for(int i=n;i;i--) ni[i-1]=ni[i]*(ll)i%ha; // prepare edges between different colors for(int i=1;i<=n;i++) if(mn[i]<mn[pos[0]]) pos[1]=pos[0],pos[0]=i; else if(mn[i]<mn[pos[1]]) pos[1]=i; pos[0]=p[pos[0]],pos[1]=p[pos[1]]; int yz=Y-w[pos[0]],yo=Y-w[pos[1]],cx=col[pos[0]],cy=col[pos[1]]; for(int i=1;i<=n;i++){ C=col[i]; if(C!=cx&&w[i]<=yz) ae(i,pos[0]); if(C!=cy&&w[i]<=yo) ae(i,pos[1]); } // prepare edges between the same colors for(int i=1;i<=n;i++){ C=col[i]; if(i!=p[C]&&w[i]+w[p[C]]<=X) ae(i,p[C]); } } void dfs(int x){ v[x]=1,sz++,C=col[x]; if(dfn[C]!=dc) dfn[C]=dc,now.pb(C),num[C]=1; else num[C]++; for(int i:g[x]) if(!v[i]) dfs(i); } inline void solve(){ // calculate for(int i=1;i<=n;i++) if(!v[i]){ dc++,now.clear(),sz=0; dfs(i),ans=ans*(ll)jc[sz]%ha; for(int j:now) ans=ans*(ll)ni[num[j]]%ha; } } int main(){ memset(mn,0x3f,sizeof(mn)),w[0]=1e9+233; n=read(),X=read(),Y=read(); for(int i=1;i<=n;i++){ C=col[i]=read(),w[i]=read(); if(w[i]<mn[C]) mn[C]=w[i],p[C]=i; } init(),solve(); printf("%d\n",ans); return 0; }
Submission Info
Submission Time | |
---|---|
Task | D - Colorful Balls |
User | JYYHH |
Language | C++14 (GCC 5.4.1) |
Score | 1000 |
Code Size | 1974 Byte |
Status | AC |
Exec Time | 78 ms |
Memory | 19304 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1000 / 1000 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 00_example_01.txt, 00_example_02.txt, 00_example_03.txt |
All | 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
00_example_01.txt | AC | 4 ms | 10112 KB |
00_example_02.txt | AC | 4 ms | 10112 KB |
00_example_03.txt | AC | 4 ms | 10112 KB |
01.txt | AC | 4 ms | 10112 KB |
02.txt | AC | 4 ms | 10112 KB |
03.txt | AC | 6 ms | 10496 KB |
04.txt | AC | 4 ms | 10112 KB |
05.txt | AC | 5 ms | 10240 KB |
06.txt | AC | 4 ms | 10240 KB |
07.txt | AC | 31 ms | 13808 KB |
08.txt | AC | 4 ms | 10112 KB |
09.txt | AC | 20 ms | 12920 KB |
10.txt | AC | 14 ms | 11904 KB |
11.txt | AC | 4 ms | 10112 KB |
12.txt | AC | 4 ms | 10112 KB |
13.txt | AC | 6 ms | 10496 KB |
14.txt | AC | 4 ms | 10112 KB |
15.txt | AC | 15 ms | 12152 KB |
16.txt | AC | 22 ms | 12032 KB |
17.txt | AC | 9 ms | 10880 KB |
18.txt | AC | 16 ms | 11776 KB |
19.txt | AC | 25 ms | 12792 KB |
20.txt | AC | 65 ms | 16496 KB |
21.txt | AC | 49 ms | 15600 KB |
22.txt | AC | 43 ms | 13568 KB |
23.txt | AC | 60 ms | 18792 KB |
24.txt | AC | 44 ms | 12800 KB |
25.txt | AC | 60 ms | 16880 KB |
26.txt | AC | 62 ms | 19176 KB |
27.txt | AC | 60 ms | 15104 KB |
28.txt | AC | 75 ms | 19304 KB |
29.txt | AC | 63 ms | 17648 KB |
30.txt | AC | 42 ms | 12032 KB |
31.txt | AC | 66 ms | 19304 KB |
32.txt | AC | 53 ms | 14456 KB |
33.txt | AC | 78 ms | 19048 KB |
34.txt | AC | 69 ms | 18800 KB |
35.txt | AC | 55 ms | 17004 KB |
36.txt | AC | 70 ms | 18168 KB |
37.txt | AC | 57 ms | 17516 KB |
38.txt | AC | 55 ms | 17004 KB |
39.txt | AC | 56 ms | 17660 KB |
40.txt | AC | 62 ms | 19176 KB |
41.txt | AC | 56 ms | 16368 KB |
42.txt | AC | 49 ms | 15600 KB |
43.txt | AC | 43 ms | 13440 KB |
44.txt | AC | 55 ms | 18920 KB |
45.txt | AC | 39 ms | 12800 KB |
46.txt | AC | 59 ms | 16624 KB |
47.txt | AC | 38 ms | 15480 KB |
48.txt | AC | 37 ms | 13048 KB |
49.txt | AC | 39 ms | 14584 KB |
50.txt | AC | 39 ms | 16500 KB |
51.txt | AC | 34 ms | 14456 KB |
52.txt | AC | 36 ms | 15480 KB |
53.txt | AC | 36 ms | 15480 KB |
54.txt | AC | 36 ms | 15480 KB |