Submission #2208056
Source Code Expand
#include <map>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
void Get_Val(int &Ret)
{
Ret = 0;
char ch;
while (ch = getchar(), ch > '9' || ch < '0')
;
do
{
(Ret *= 10) += ch - '0';
}
while (ch = getchar(), ch >= '0' && ch <= '9');
}
const int Max_N(200050);
typedef long long int LL;
const int MOD(1000000000 + 7);
inline int Mult(const int &a, const int &b)
{
return (a * 1LL) * b % MOD;
}
void exgcd(const int &a, const int &b, int &x, int &y)
{
if (b == 0)
x = 1, y = 0;
else
exgcd(b, a % b, y, x), y -= x * (a / b);
}
inline int inverse(const int &a)
{
int invx, invy;
exgcd(a, MOD, invx, invy);
return (invx % MOD + MOD) % MOD;
}
struct node
{
node(const int &_pos = 0, const int &_w = 0) : pos(_pos), w(_w) {}
int pos, w;
};
int N, X, Y, Father[Max_N], Fac[Max_N], Inv[Max_N];
map<int, int> S[Max_N];
vector<node> V[Max_N];
int Get_Father(const int &x)
{
return Father[x] == x ? x : Father[x] = Get_Father(Father[x]);
}
inline void Link(const int &u, const int &v)
{
Father[Get_Father(u)] = Get_Father(v);
}
inline bool comp(const node &a, const node &b)
{
return a.w < b.w;
}
void init()
{
Get_Val(N), Get_Val(X), Get_Val(Y);
for (int i = 1, c, w;i <= N;++i)
Father[i] = i, Get_Val(c), Get_Val(w), V[c].push_back(node(i, w));
for (int c = 1;c <= N;++c)
sort(V[c].begin(), V[c].end(), comp);
Fac[0] = 1;
for (int i = 1;i <= N;++i)
Fac[i] = Mult(Fac[i - 1], i);
Inv[N] = inverse(Fac[N]);
for (int i = N - 1;i >= 0;--i)
Inv[i] = Mult(Inv[i + 1], i + 1);
}
//把每个球当做一个点,某个球当前所在的位置是该点的权值。我们将能够交换位置的两个球之间连一条边,那么有边的两个点之间可以交换权值
//考虑两个都在同一个联通块中的点1和n,他们之间的路径是1, 2, 3, ..., n
//可以形如1, 2, 3, ..., n -> 2, 3, ..., n, 1 -> n, 2, 3, ..., 1的方式交换,来做到交换两个点的权值而不影响其它点的权值
//可以只考虑联通块中W最小的点来优化连边
//最后每个联通块都是独立的,可以分开做
void makegraph()
{
for (int c = 1;c <= N;++c)
for (int i = 1;i < V[c].size();++i)
if (V[c][i].w + V[c][0].w <= X)
Link(V[c][i].pos, V[c][0].pos);
int Minpos(-1), Minw, Minc;
for (int c = 1;c <= N;++c)
for (int i = 0;i < V[c].size();++i)
if (Minpos == -1 || V[c][i].w < Minw)
Minpos = V[c][i].pos, Minw = V[c][i].w, Minc = c;
for (int c = 1;c <= N;++c)
if (c != Minc)
for (int i = 0;i < V[c].size();++i)
if (V[c][i].w + Minw <= Y)
Link(V[c][i].pos, Minpos);
Minpos = -1;
for (int c = 1;c <= N;++c)
if (c != Minc)
for (int i = 0;i < V[c].size();++i)
if (Minpos == -1 || V[c][i].w < Minw)
Minpos = V[c][i].pos, Minw = V[c][i].w;
if (Minpos == -1)
return;
for (int i = 0;i < V[Minc].size();++i)
if (V[Minc][i].w + Minw <= Y)
Link(V[Minc][i].pos, Minpos);
}
int js(const int &pos)
{
int Sum(0), Ans(1);
for (map<int, int>::iterator it = S[pos].begin();it != S[pos].end();++it)
Ans = Mult(Ans, Inv[it -> second]), Sum += it -> second;
return Mult(Ans, Fac[Sum]);
}
void work()
{
for (int c = 1;c <= N;++c)
for (int i = 0;i < V[c].size();++i)
++S[Get_Father(V[c][i].pos)][c];
int Ans(1);
for (int i = 1;i <= N;++i)
if (S[i].size())
Ans = Mult(Ans, js(i));
printf("%d", Ans);
}
int main()
{
init();
makegraph();
work();
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Colorful Balls |
User |
Created_equal |
Language |
C++14 (GCC 5.4.1) |
Score |
1000 |
Code Size |
3559 Byte |
Status |
AC |
Exec Time |
108 ms |
Memory |
30464 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 |
6 ms |
16640 KB |
00_example_02.txt |
AC |
6 ms |
16640 KB |
00_example_03.txt |
AC |
6 ms |
16640 KB |
01.txt |
AC |
6 ms |
16640 KB |
02.txt |
AC |
6 ms |
16640 KB |
03.txt |
AC |
8 ms |
16896 KB |
04.txt |
AC |
6 ms |
16640 KB |
05.txt |
AC |
7 ms |
16768 KB |
06.txt |
AC |
7 ms |
16640 KB |
07.txt |
AC |
41 ms |
20864 KB |
08.txt |
AC |
7 ms |
16640 KB |
09.txt |
AC |
19 ms |
17408 KB |
10.txt |
AC |
16 ms |
17536 KB |
11.txt |
AC |
7 ms |
16640 KB |
12.txt |
AC |
6 ms |
16640 KB |
13.txt |
AC |
9 ms |
17024 KB |
14.txt |
AC |
6 ms |
16640 KB |
15.txt |
AC |
22 ms |
18816 KB |
16.txt |
AC |
49 ms |
22400 KB |
17.txt |
AC |
11 ms |
17024 KB |
18.txt |
AC |
19 ms |
17920 KB |
19.txt |
AC |
33 ms |
19840 KB |
20.txt |
AC |
101 ms |
28160 KB |
21.txt |
AC |
108 ms |
29440 KB |
22.txt |
AC |
104 ms |
29184 KB |
23.txt |
AC |
97 ms |
27776 KB |
24.txt |
AC |
108 ms |
30080 KB |
25.txt |
AC |
102 ms |
27648 KB |
26.txt |
AC |
93 ms |
27520 KB |
27.txt |
AC |
91 ms |
27008 KB |
28.txt |
AC |
95 ms |
27520 KB |
29.txt |
AC |
97 ms |
27904 KB |
30.txt |
AC |
106 ms |
30208 KB |
31.txt |
AC |
95 ms |
27392 KB |
32.txt |
AC |
99 ms |
28672 KB |
33.txt |
AC |
96 ms |
27648 KB |
34.txt |
AC |
85 ms |
26496 KB |
35.txt |
AC |
56 ms |
20864 KB |
36.txt |
AC |
69 ms |
21248 KB |
37.txt |
AC |
58 ms |
21120 KB |
38.txt |
AC |
56 ms |
20608 KB |
39.txt |
AC |
61 ms |
20864 KB |
40.txt |
AC |
84 ms |
27776 KB |
41.txt |
AC |
87 ms |
28928 KB |
42.txt |
AC |
88 ms |
29952 KB |
43.txt |
AC |
84 ms |
29696 KB |
44.txt |
AC |
85 ms |
28544 KB |
45.txt |
AC |
86 ms |
30464 KB |
46.txt |
AC |
85 ms |
28416 KB |
47.txt |
AC |
63 ms |
21360 KB |
48.txt |
AC |
75 ms |
24304 KB |
49.txt |
AC |
69 ms |
22256 KB |
50.txt |
AC |
54 ms |
25204 KB |
51.txt |
AC |
52 ms |
25204 KB |
52.txt |
AC |
57 ms |
22388 KB |
53.txt |
AC |
60 ms |
22388 KB |
54.txt |
AC |
57 ms |
22388 KB |