답안 #469104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
469104 2021-08-30T18:09:28 Z sinamhdv Subtree (INOI20_subtree) C++11
100 / 100
181 ms 27740 KB
// INOI20_subtree
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;

#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define fast_io ios::sync_with_stdio(0); cin.tie(0);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fr first
#define sc second
#define endl '\n'

const int MAXN = 100100;
const int MAXE = 150100;

int n, m;
vector<int> adj[MAXN];
int ef[MAXE], eto[MAXE];
int hei[MAXN];
bool iscut[MAXE];
bool mark[MAXN];
vector<int> comp[MAXN];
int cn, cnum[MAXN];
vector<int> tree[MAXN];
int head[MAXN], vpar[MAXN];
int dp[MAXN][2];
int wei[MAXN];

inline void addedge(int x, int y)
{
	tree[x].pb(y);
	tree[y].pb(x);
}

inline int dfs1(int v, int epar)
{
	mark[v] = true;
	int dp = hei[v];
	for (int e : adj[v]) if (e != epar)
	{
		int u = eto[e] ^ ef[e] ^ v;
		if (mark[u]) dp = min(dp, hei[u]);
		else hei[u] = hei[v] + 1, dp = min(dp, dfs1(u, e));
	}
	if (dp >= hei[v] && epar >= 0) iscut[epar] = true;
	return dp;
}

void dfs2(int v)
{
	cnum[v] = cn;
	comp[cn].pb(v);
	for (int e : adj[v]) if (!iscut[e])
	{
		int u = ef[e] ^ eto[e] ^ v;
		if (!cnum[u]) dfs2(u);
	}
}

void dfs3(int v)
{
	mark[v] = true;
	for (int e : adj[v])
	{
		int u = ef[e] ^ eto[e] ^ v;
		if (mark[u]) continue;
		if (iscut[e])
		{
			head[cnum[u]] = u;
			vpar[cnum[u]] = v;
		}
		dfs3(u);
	}
}

void dfs4(int v, int par)
{
	for (int u : tree[v]) if (u != par)
	{
		dfs4(u, v);
		wei[vpar[u]] = (wei[vpar[u]] * ((ll)dp[u][1] + 1)) % mod;
	}

	// calc dp
	if ((int)comp[v].size() == 1)	// single vertex
	{
		dp[v][0] = dp[v][1] = wei[comp[v][0]];
		return;
	}
	
	// cycle
	int k = (int)comp[v].size();
	vector<int> ps(k + 5);
	
	ps[0] = 0;
	int cur = 1;
	FOR(i, 1, k + 1)
	{
		cur = (ll)cur * wei[comp[v][i - 1]] % mod;
		ps[i] = (ps[i - 1] + cur) % mod;
	}

	dbgr(ps.begin(), ps.end());

	cur = 1;
	for (int i = k - 1; i >= 1; i--)
	{
		dp[v][1] = (dp[v][1] + (ll)ps[i] * cur) % mod;
		cur = (ll)cur * wei[comp[v][i]] % mod;
	}

	dp[v][0] = dp[v][1];
	cur = 0;
	FOR(i, 1, k)
	{
		cur = wei[comp[v][i]] * ((ll)cur + 1) % mod;
		dp[v][0] = (dp[v][0] + cur) % mod;
	}
}

int32_t main(void)
{
	fast_io;
	cin >> n >> m;
	FOR(i, 0, m)
	{
		cin >> ef[i] >> eto[i];
		adj[ef[i]].pb(i);
		adj[eto[i]].pb(i);
	}

	dfs1(1, -1);
	
	dbgr(iscut, iscut + m);

	FOR(i, 1, n + 1) if (!cnum[i]) cn++, dfs2(i);

	memset(mark, 0, sizeof(mark));
	dfs3(1);
	head[cnum[1]] = 1;

	FOR(i, 0, m) if (iscut[i]) addedge(cnum[ef[i]], cnum[eto[i]]);

	FOR(i, 1, cn + 1)
	{
		rotate(comp[i].begin(), find(all(comp[i]), head[i]), comp[i].end());
	}

	dbg(cn);
	FOR(i, 1, cn + 1)
	{
		dbg(i);
		dbg(head[i]);
		dbg(vpar[i]);
		dbgr(comp[i].begin(), comp[i].end());
	}

	fill(wei + 1, wei + n + 1, 1);
	dfs4(cnum[1], -1);

#ifdef DEBUG
	dbgr(wei + 1, wei + n + 1);
	FOR(i, 1, cn + 1) cout << "(" << dp[i][0] << ", " << dp[i][1] << ") ";
	cout << endl;
#endif

	ll ans = 0;
	FOR(i, 1, cn + 1) ans = (ans + dp[i][0]) % mod;
	ans = (mod + ans % mod) % mod;
	cout << ans << endl;

	return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7500 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7500 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7400 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7488 KB Output is correct
8 Correct 4 ms 7500 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7500 KB Output is correct
11 Correct 4 ms 7488 KB Output is correct
12 Correct 4 ms 7500 KB Output is correct
13 Correct 4 ms 7500 KB Output is correct
14 Correct 5 ms 7500 KB Output is correct
15 Correct 4 ms 7440 KB Output is correct
16 Correct 4 ms 7500 KB Output is correct
17 Correct 4 ms 7500 KB Output is correct
18 Correct 4 ms 7500 KB Output is correct
19 Correct 4 ms 7500 KB Output is correct
20 Correct 4 ms 7500 KB Output is correct
21 Correct 4 ms 7500 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 7500 KB Output is correct
24 Correct 4 ms 7500 KB Output is correct
25 Correct 4 ms 7500 KB Output is correct
26 Correct 5 ms 7416 KB Output is correct
27 Correct 4 ms 7500 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 4 ms 7500 KB Output is correct
31 Correct 4 ms 7500 KB Output is correct
32 Correct 4 ms 7500 KB Output is correct
33 Correct 4 ms 7500 KB Output is correct
34 Correct 4 ms 7500 KB Output is correct
35 Correct 4 ms 7372 KB Output is correct
36 Correct 4 ms 7500 KB Output is correct
37 Correct 4 ms 7500 KB Output is correct
38 Correct 4 ms 7500 KB Output is correct
39 Correct 4 ms 7500 KB Output is correct
40 Correct 4 ms 7500 KB Output is correct
41 Correct 4 ms 7500 KB Output is correct
42 Correct 4 ms 7500 KB Output is correct
43 Correct 4 ms 7500 KB Output is correct
44 Correct 4 ms 7500 KB Output is correct
45 Correct 4 ms 7500 KB Output is correct
46 Correct 5 ms 7500 KB Output is correct
47 Correct 4 ms 7500 KB Output is correct
48 Correct 4 ms 7372 KB Output is correct
49 Correct 4 ms 7500 KB Output is correct
50 Correct 4 ms 7500 KB Output is correct
51 Correct 5 ms 7500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7500 KB Output is correct
2 Correct 12 ms 8764 KB Output is correct
3 Correct 126 ms 20644 KB Output is correct
4 Correct 144 ms 20692 KB Output is correct
5 Correct 150 ms 20676 KB Output is correct
6 Correct 121 ms 20652 KB Output is correct
7 Correct 148 ms 26016 KB Output is correct
8 Correct 136 ms 24044 KB Output is correct
9 Correct 129 ms 24156 KB Output is correct
10 Correct 97 ms 21600 KB Output is correct
11 Correct 109 ms 21212 KB Output is correct
12 Correct 154 ms 20460 KB Output is correct
13 Correct 143 ms 20468 KB Output is correct
14 Correct 96 ms 23484 KB Output is correct
15 Correct 139 ms 26980 KB Output is correct
16 Correct 160 ms 27740 KB Output is correct
17 Correct 140 ms 20436 KB Output is correct
18 Correct 131 ms 20524 KB Output is correct
19 Correct 181 ms 20612 KB Output is correct
20 Correct 95 ms 21308 KB Output is correct
21 Correct 95 ms 21420 KB Output is correct
22 Correct 98 ms 21252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7500 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7500 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7400 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7488 KB Output is correct
8 Correct 4 ms 7500 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7500 KB Output is correct
11 Correct 4 ms 7488 KB Output is correct
12 Correct 4 ms 7500 KB Output is correct
13 Correct 4 ms 7500 KB Output is correct
14 Correct 5 ms 7500 KB Output is correct
15 Correct 4 ms 7440 KB Output is correct
16 Correct 4 ms 7500 KB Output is correct
17 Correct 4 ms 7500 KB Output is correct
18 Correct 4 ms 7500 KB Output is correct
19 Correct 4 ms 7500 KB Output is correct
20 Correct 4 ms 7500 KB Output is correct
21 Correct 4 ms 7500 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 7500 KB Output is correct
24 Correct 4 ms 7500 KB Output is correct
25 Correct 4 ms 7500 KB Output is correct
26 Correct 5 ms 7416 KB Output is correct
27 Correct 4 ms 7500 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 4 ms 7500 KB Output is correct
31 Correct 4 ms 7500 KB Output is correct
32 Correct 4 ms 7500 KB Output is correct
33 Correct 4 ms 7500 KB Output is correct
34 Correct 4 ms 7500 KB Output is correct
35 Correct 4 ms 7372 KB Output is correct
36 Correct 4 ms 7500 KB Output is correct
37 Correct 4 ms 7500 KB Output is correct
38 Correct 4 ms 7500 KB Output is correct
39 Correct 4 ms 7500 KB Output is correct
40 Correct 4 ms 7500 KB Output is correct
41 Correct 4 ms 7500 KB Output is correct
42 Correct 4 ms 7500 KB Output is correct
43 Correct 4 ms 7500 KB Output is correct
44 Correct 4 ms 7500 KB Output is correct
45 Correct 4 ms 7500 KB Output is correct
46 Correct 5 ms 7500 KB Output is correct
47 Correct 4 ms 7500 KB Output is correct
48 Correct 4 ms 7372 KB Output is correct
49 Correct 4 ms 7500 KB Output is correct
50 Correct 4 ms 7500 KB Output is correct
51 Correct 5 ms 7500 KB Output is correct
52 Correct 8 ms 8012 KB Output is correct
53 Correct 8 ms 8140 KB Output is correct
54 Correct 8 ms 8012 KB Output is correct
55 Correct 8 ms 8152 KB Output is correct
56 Correct 9 ms 8168 KB Output is correct
57 Correct 8 ms 8140 KB Output is correct
58 Correct 7 ms 8160 KB Output is correct
59 Correct 7 ms 8140 KB Output is correct
60 Correct 7 ms 8168 KB Output is correct
61 Correct 7 ms 8012 KB Output is correct
62 Correct 8 ms 7908 KB Output is correct
63 Correct 7 ms 8140 KB Output is correct
64 Correct 7 ms 8140 KB Output is correct
65 Correct 8 ms 8140 KB Output is correct
66 Correct 8 ms 8036 KB Output is correct
67 Correct 8 ms 8140 KB Output is correct
68 Correct 8 ms 8140 KB Output is correct
69 Correct 9 ms 8012 KB Output is correct
70 Correct 7 ms 8164 KB Output is correct
71 Correct 8 ms 8012 KB Output is correct
72 Correct 8 ms 7884 KB Output is correct
73 Correct 7 ms 8160 KB Output is correct
74 Correct 8 ms 7908 KB Output is correct
75 Correct 8 ms 7912 KB Output is correct
76 Correct 8 ms 8012 KB Output is correct
77 Correct 9 ms 8200 KB Output is correct
78 Correct 8 ms 7908 KB Output is correct
79 Correct 7 ms 8012 KB Output is correct
80 Correct 7 ms 7888 KB Output is correct
81 Correct 7 ms 8012 KB Output is correct
82 Correct 10 ms 8012 KB Output is correct
83 Correct 9 ms 8012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7500 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7500 KB Output is correct
4 Correct 4 ms 7500 KB Output is correct
5 Correct 5 ms 7400 KB Output is correct
6 Correct 4 ms 7500 KB Output is correct
7 Correct 4 ms 7488 KB Output is correct
8 Correct 4 ms 7500 KB Output is correct
9 Correct 4 ms 7372 KB Output is correct
10 Correct 4 ms 7500 KB Output is correct
11 Correct 4 ms 7488 KB Output is correct
12 Correct 4 ms 7500 KB Output is correct
13 Correct 4 ms 7500 KB Output is correct
14 Correct 5 ms 7500 KB Output is correct
15 Correct 4 ms 7440 KB Output is correct
16 Correct 4 ms 7500 KB Output is correct
17 Correct 4 ms 7500 KB Output is correct
18 Correct 4 ms 7500 KB Output is correct
19 Correct 4 ms 7500 KB Output is correct
20 Correct 4 ms 7500 KB Output is correct
21 Correct 4 ms 7500 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 7500 KB Output is correct
24 Correct 4 ms 7500 KB Output is correct
25 Correct 4 ms 7500 KB Output is correct
26 Correct 5 ms 7416 KB Output is correct
27 Correct 4 ms 7500 KB Output is correct
28 Correct 4 ms 7500 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 4 ms 7500 KB Output is correct
31 Correct 4 ms 7500 KB Output is correct
32 Correct 4 ms 7500 KB Output is correct
33 Correct 4 ms 7500 KB Output is correct
34 Correct 4 ms 7500 KB Output is correct
35 Correct 4 ms 7372 KB Output is correct
36 Correct 4 ms 7500 KB Output is correct
37 Correct 4 ms 7500 KB Output is correct
38 Correct 4 ms 7500 KB Output is correct
39 Correct 4 ms 7500 KB Output is correct
40 Correct 4 ms 7500 KB Output is correct
41 Correct 4 ms 7500 KB Output is correct
42 Correct 4 ms 7500 KB Output is correct
43 Correct 4 ms 7500 KB Output is correct
44 Correct 4 ms 7500 KB Output is correct
45 Correct 4 ms 7500 KB Output is correct
46 Correct 5 ms 7500 KB Output is correct
47 Correct 4 ms 7500 KB Output is correct
48 Correct 4 ms 7372 KB Output is correct
49 Correct 4 ms 7500 KB Output is correct
50 Correct 4 ms 7500 KB Output is correct
51 Correct 5 ms 7500 KB Output is correct
52 Correct 4 ms 7500 KB Output is correct
53 Correct 12 ms 8764 KB Output is correct
54 Correct 126 ms 20644 KB Output is correct
55 Correct 144 ms 20692 KB Output is correct
56 Correct 150 ms 20676 KB Output is correct
57 Correct 121 ms 20652 KB Output is correct
58 Correct 148 ms 26016 KB Output is correct
59 Correct 136 ms 24044 KB Output is correct
60 Correct 129 ms 24156 KB Output is correct
61 Correct 97 ms 21600 KB Output is correct
62 Correct 109 ms 21212 KB Output is correct
63 Correct 154 ms 20460 KB Output is correct
64 Correct 143 ms 20468 KB Output is correct
65 Correct 96 ms 23484 KB Output is correct
66 Correct 139 ms 26980 KB Output is correct
67 Correct 160 ms 27740 KB Output is correct
68 Correct 140 ms 20436 KB Output is correct
69 Correct 131 ms 20524 KB Output is correct
70 Correct 181 ms 20612 KB Output is correct
71 Correct 95 ms 21308 KB Output is correct
72 Correct 95 ms 21420 KB Output is correct
73 Correct 98 ms 21252 KB Output is correct
74 Correct 8 ms 8012 KB Output is correct
75 Correct 8 ms 8140 KB Output is correct
76 Correct 8 ms 8012 KB Output is correct
77 Correct 8 ms 8152 KB Output is correct
78 Correct 9 ms 8168 KB Output is correct
79 Correct 8 ms 8140 KB Output is correct
80 Correct 7 ms 8160 KB Output is correct
81 Correct 7 ms 8140 KB Output is correct
82 Correct 7 ms 8168 KB Output is correct
83 Correct 7 ms 8012 KB Output is correct
84 Correct 8 ms 7908 KB Output is correct
85 Correct 7 ms 8140 KB Output is correct
86 Correct 7 ms 8140 KB Output is correct
87 Correct 8 ms 8140 KB Output is correct
88 Correct 8 ms 8036 KB Output is correct
89 Correct 8 ms 8140 KB Output is correct
90 Correct 8 ms 8140 KB Output is correct
91 Correct 9 ms 8012 KB Output is correct
92 Correct 7 ms 8164 KB Output is correct
93 Correct 8 ms 8012 KB Output is correct
94 Correct 8 ms 7884 KB Output is correct
95 Correct 7 ms 8160 KB Output is correct
96 Correct 8 ms 7908 KB Output is correct
97 Correct 8 ms 7912 KB Output is correct
98 Correct 8 ms 8012 KB Output is correct
99 Correct 9 ms 8200 KB Output is correct
100 Correct 8 ms 7908 KB Output is correct
101 Correct 7 ms 8012 KB Output is correct
102 Correct 7 ms 7888 KB Output is correct
103 Correct 7 ms 8012 KB Output is correct
104 Correct 10 ms 8012 KB Output is correct
105 Correct 9 ms 8012 KB Output is correct
106 Correct 126 ms 21448 KB Output is correct
107 Correct 149 ms 20752 KB Output is correct
108 Correct 133 ms 20460 KB Output is correct
109 Correct 150 ms 20212 KB Output is correct
110 Correct 108 ms 22596 KB Output is correct
111 Correct 108 ms 22476 KB Output is correct
112 Correct 125 ms 21344 KB Output is correct
113 Correct 128 ms 17904 KB Output is correct
114 Correct 128 ms 19576 KB Output is correct
115 Correct 91 ms 22332 KB Output is correct
116 Correct 116 ms 22016 KB Output is correct
117 Correct 127 ms 21540 KB Output is correct
118 Correct 101 ms 22328 KB Output is correct
119 Correct 132 ms 18156 KB Output is correct
120 Correct 114 ms 17824 KB Output is correct
121 Correct 115 ms 17516 KB Output is correct
122 Correct 137 ms 17368 KB Output is correct
123 Correct 96 ms 22640 KB Output is correct
124 Correct 92 ms 22576 KB Output is correct
125 Correct 99 ms 22520 KB Output is correct
126 Correct 133 ms 21132 KB Output is correct
127 Correct 138 ms 20676 KB Output is correct
128 Correct 118 ms 20416 KB Output is correct
129 Correct 135 ms 20224 KB Output is correct
130 Correct 106 ms 22468 KB Output is correct
131 Correct 110 ms 22564 KB Output is correct
132 Correct 120 ms 21188 KB Output is correct
133 Correct 115 ms 17988 KB Output is correct
134 Correct 134 ms 19552 KB Output is correct
135 Correct 87 ms 22276 KB Output is correct
136 Correct 116 ms 21036 KB Output is correct
137 Correct 168 ms 21552 KB Output is correct
138 Correct 88 ms 22368 KB Output is correct
139 Correct 106 ms 18068 KB Output is correct
140 Correct 111 ms 16428 KB Output is correct
141 Correct 152 ms 16164 KB Output is correct
142 Correct 120 ms 15976 KB Output is correct
143 Correct 95 ms 21456 KB Output is correct
144 Correct 88 ms 21480 KB Output is correct
145 Correct 88 ms 21168 KB Output is correct
146 Correct 104 ms 14072 KB Output is correct
147 Correct 104 ms 13440 KB Output is correct
148 Correct 102 ms 13512 KB Output is correct
149 Correct 88 ms 14232 KB Output is correct
150 Correct 121 ms 21020 KB Output is correct
151 Correct 128 ms 14148 KB Output is correct
152 Correct 126 ms 15432 KB Output is correct
153 Correct 112 ms 15348 KB Output is correct
154 Correct 103 ms 16836 KB Output is correct
155 Correct 91 ms 21128 KB Output is correct
156 Correct 118 ms 16860 KB Output is correct
157 Correct 99 ms 16540 KB Output is correct
158 Correct 138 ms 16104 KB Output is correct
159 Correct 158 ms 20264 KB Output is correct
160 Correct 151 ms 24644 KB Output is correct
161 Correct 147 ms 19888 KB Output is correct
162 Correct 96 ms 20704 KB Output is correct