답안 #1003593

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003593 2024-06-20T13:30:16 Z aykhn Subtree (INOI20_subtree) C++17
100 / 100
46 ms 22640 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
 
const int MXN = 1e5 + 5;
const int LOG = 20;
const int mod = 1e9 + 7;

vector<int> adj[MXN];
int p[MXN], used[MXN], down[MXN];
int in[MXN];
int dp[MXN];

void dfs(int a)
{
  dp[a] = 0;
  used[a] = 1;
  for (int &v : adj[a])
  {
    if (!used[v]) 
    {
      p[v] = a;
      dfs(v);
    }
    else if (v != p[a]) 
    {
      assert(!down[v]);
      down[v] = a;
    }
  }
  if (down[a])
  {
    vector<int> path;
    int x = down[a];
    while (x)
    {
      path.push_back(x);
      in[x] = 1;
      if (x == a) break;
      x = p[x];
    }
    reverse(path.begin(), path.end());
    vector<int> sum(path.size(), 0);
    int co = 1;
    for (int i = (int)path.size() - 1; i >= 0; i--)
    {
      if (i == (int)path.size() - 1) co = (co * dp[path[i]]) % mod;
      else
      {
        for (int &v : adj[path[i]]) 
        {
          if (!in[v] && v != p[path[i]]) co = (co * (dp[v] + 1)) % mod;
        }
      }
      sum[i] = ((i + 1 < path.size() ? sum[i + 1] : 0) + co) % mod;
    }
    co = 1;
    for (int i = 0; i + 1 < path.size(); i++)
    {
      for (int &v : adj[path[i]])
      {
        if (!in[v] && v != p[path[i]]) 
        {
          co = (co * (dp[v] + 1)) % mod;
        }
      }
      dp[a] = (dp[a] + co) % mod;
      if (i + 2 < path.size()) dp[a] = (dp[a] + ((sum[i + 2] * co) % mod)) % mod;
    }
    for (int x : path) in[x] = 0;
  }
  else
  {
    dp[a] = 1;
    for (int &v : adj[a])
    {
      dp[a] = (dp[a] * (dp[v] + 1)) % mod;
    }
  }
}

signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
  {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  int res = 0;
  for (int i = 1; i <= n; i++)
  {
    res = (res + dp[i]) % mod;
  }
  cout << res << '\n';
}

Compilation message

Main.cpp: In function 'void dfs(long long int)':
Main.cpp:58:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |       sum[i] = ((i + 1 < path.size() ? sum[i + 1] : 0) + co) % mod;
      |                  ~~~~~~^~~~~~~~~~~~~
Main.cpp:61:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i + 1 < path.size(); i++)
      |                     ~~~~~~^~~~~~~~~~~~~
Main.cpp:71:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |       if (i + 2 < path.size()) dp[a] = (dp[a] + ((sum[i + 2] * co) % mod)) % mod;
      |           ~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2648 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 1 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2592 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 3420 KB Output is correct
3 Correct 32 ms 8796 KB Output is correct
4 Correct 27 ms 8784 KB Output is correct
5 Correct 26 ms 8892 KB Output is correct
6 Correct 27 ms 8792 KB Output is correct
7 Correct 44 ms 19548 KB Output is correct
8 Correct 35 ms 15840 KB Output is correct
9 Correct 31 ms 15964 KB Output is correct
10 Correct 23 ms 9308 KB Output is correct
11 Correct 22 ms 8628 KB Output is correct
12 Correct 26 ms 8244 KB Output is correct
13 Correct 29 ms 8284 KB Output is correct
14 Correct 27 ms 13512 KB Output is correct
15 Correct 39 ms 21332 KB Output is correct
16 Correct 35 ms 22640 KB Output is correct
17 Correct 27 ms 8284 KB Output is correct
18 Correct 27 ms 8284 KB Output is correct
19 Correct 28 ms 8284 KB Output is correct
20 Correct 23 ms 8912 KB Output is correct
21 Correct 21 ms 9052 KB Output is correct
22 Correct 19 ms 8936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2648 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 1 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2592 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 2 ms 3164 KB Output is correct
53 Correct 2 ms 3112 KB Output is correct
54 Correct 3 ms 3164 KB Output is correct
55 Correct 3 ms 3164 KB Output is correct
56 Correct 2 ms 3676 KB Output is correct
57 Correct 2 ms 2972 KB Output is correct
58 Correct 2 ms 3164 KB Output is correct
59 Correct 2 ms 3420 KB Output is correct
60 Correct 2 ms 3164 KB Output is correct
61 Correct 2 ms 3164 KB Output is correct
62 Correct 3 ms 3164 KB Output is correct
63 Correct 2 ms 3164 KB Output is correct
64 Correct 2 ms 3164 KB Output is correct
65 Correct 2 ms 3164 KB Output is correct
66 Correct 2 ms 3164 KB Output is correct
67 Correct 3 ms 3060 KB Output is correct
68 Correct 3 ms 3676 KB Output is correct
69 Correct 2 ms 3164 KB Output is correct
70 Correct 2 ms 3164 KB Output is correct
71 Correct 3 ms 3416 KB Output is correct
72 Correct 3 ms 3164 KB Output is correct
73 Correct 2 ms 3164 KB Output is correct
74 Correct 2 ms 3164 KB Output is correct
75 Correct 2 ms 3420 KB Output is correct
76 Correct 3 ms 3164 KB Output is correct
77 Correct 2 ms 3676 KB Output is correct
78 Correct 3 ms 3164 KB Output is correct
79 Correct 3 ms 3672 KB Output is correct
80 Correct 2 ms 3164 KB Output is correct
81 Correct 2 ms 3164 KB Output is correct
82 Correct 2 ms 3164 KB Output is correct
83 Correct 2 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2648 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 2 ms 2648 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2648 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2648 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2652 KB Output is correct
39 Correct 1 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 1 ms 2652 KB Output is correct
43 Correct 1 ms 2652 KB Output is correct
44 Correct 1 ms 2652 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2592 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2652 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 4 ms 3420 KB Output is correct
54 Correct 32 ms 8796 KB Output is correct
55 Correct 27 ms 8784 KB Output is correct
56 Correct 26 ms 8892 KB Output is correct
57 Correct 27 ms 8792 KB Output is correct
58 Correct 44 ms 19548 KB Output is correct
59 Correct 35 ms 15840 KB Output is correct
60 Correct 31 ms 15964 KB Output is correct
61 Correct 23 ms 9308 KB Output is correct
62 Correct 22 ms 8628 KB Output is correct
63 Correct 26 ms 8244 KB Output is correct
64 Correct 29 ms 8284 KB Output is correct
65 Correct 27 ms 13512 KB Output is correct
66 Correct 39 ms 21332 KB Output is correct
67 Correct 35 ms 22640 KB Output is correct
68 Correct 27 ms 8284 KB Output is correct
69 Correct 27 ms 8284 KB Output is correct
70 Correct 28 ms 8284 KB Output is correct
71 Correct 23 ms 8912 KB Output is correct
72 Correct 21 ms 9052 KB Output is correct
73 Correct 19 ms 8936 KB Output is correct
74 Correct 2 ms 3164 KB Output is correct
75 Correct 2 ms 3112 KB Output is correct
76 Correct 3 ms 3164 KB Output is correct
77 Correct 3 ms 3164 KB Output is correct
78 Correct 2 ms 3676 KB Output is correct
79 Correct 2 ms 2972 KB Output is correct
80 Correct 2 ms 3164 KB Output is correct
81 Correct 2 ms 3420 KB Output is correct
82 Correct 2 ms 3164 KB Output is correct
83 Correct 2 ms 3164 KB Output is correct
84 Correct 3 ms 3164 KB Output is correct
85 Correct 2 ms 3164 KB Output is correct
86 Correct 2 ms 3164 KB Output is correct
87 Correct 2 ms 3164 KB Output is correct
88 Correct 2 ms 3164 KB Output is correct
89 Correct 3 ms 3060 KB Output is correct
90 Correct 3 ms 3676 KB Output is correct
91 Correct 2 ms 3164 KB Output is correct
92 Correct 2 ms 3164 KB Output is correct
93 Correct 3 ms 3416 KB Output is correct
94 Correct 3 ms 3164 KB Output is correct
95 Correct 2 ms 3164 KB Output is correct
96 Correct 2 ms 3164 KB Output is correct
97 Correct 2 ms 3420 KB Output is correct
98 Correct 3 ms 3164 KB Output is correct
99 Correct 2 ms 3676 KB Output is correct
100 Correct 3 ms 3164 KB Output is correct
101 Correct 3 ms 3672 KB Output is correct
102 Correct 2 ms 3164 KB Output is correct
103 Correct 2 ms 3164 KB Output is correct
104 Correct 2 ms 3164 KB Output is correct
105 Correct 2 ms 3164 KB Output is correct
106 Correct 28 ms 10420 KB Output is correct
107 Correct 33 ms 10352 KB Output is correct
108 Correct 34 ms 10588 KB Output is correct
109 Correct 32 ms 10588 KB Output is correct
110 Correct 23 ms 10076 KB Output is correct
111 Correct 29 ms 12108 KB Output is correct
112 Correct 41 ms 19548 KB Output is correct
113 Correct 34 ms 10076 KB Output is correct
114 Correct 36 ms 10332 KB Output is correct
115 Correct 22 ms 8908 KB Output is correct
116 Correct 38 ms 20304 KB Output is correct
117 Correct 42 ms 19248 KB Output is correct
118 Correct 31 ms 11716 KB Output is correct
119 Correct 33 ms 10076 KB Output is correct
120 Correct 41 ms 10356 KB Output is correct
121 Correct 37 ms 10844 KB Output is correct
122 Correct 40 ms 10844 KB Output is correct
123 Correct 24 ms 9492 KB Output is correct
124 Correct 22 ms 9052 KB Output is correct
125 Correct 24 ms 9060 KB Output is correct
126 Correct 32 ms 10360 KB Output is correct
127 Correct 35 ms 10540 KB Output is correct
128 Correct 32 ms 10428 KB Output is correct
129 Correct 35 ms 10528 KB Output is correct
130 Correct 24 ms 9944 KB Output is correct
131 Correct 29 ms 12168 KB Output is correct
132 Correct 46 ms 19280 KB Output is correct
133 Correct 32 ms 10076 KB Output is correct
134 Correct 30 ms 10328 KB Output is correct
135 Correct 20 ms 8912 KB Output is correct
136 Correct 37 ms 17996 KB Output is correct
137 Correct 39 ms 18512 KB Output is correct
138 Correct 30 ms 11724 KB Output is correct
139 Correct 38 ms 10068 KB Output is correct
140 Correct 35 ms 10332 KB Output is correct
141 Correct 37 ms 10832 KB Output is correct
142 Correct 41 ms 10840 KB Output is correct
143 Correct 24 ms 9304 KB Output is correct
144 Correct 21 ms 9052 KB Output is correct
145 Correct 20 ms 8908 KB Output is correct
146 Correct 35 ms 10444 KB Output is correct
147 Correct 31 ms 9968 KB Output is correct
148 Correct 33 ms 10324 KB Output is correct
149 Correct 36 ms 11452 KB Output is correct
150 Correct 42 ms 21788 KB Output is correct
151 Correct 39 ms 10332 KB Output is correct
152 Correct 39 ms 11100 KB Output is correct
153 Correct 39 ms 11088 KB Output is correct
154 Correct 32 ms 11524 KB Output is correct
155 Correct 24 ms 10840 KB Output is correct
156 Correct 39 ms 18704 KB Output is correct
157 Correct 37 ms 17520 KB Output is correct
158 Correct 40 ms 16252 KB Output is correct
159 Correct 28 ms 9808 KB Output is correct
160 Correct 34 ms 20052 KB Output is correct
161 Correct 31 ms 9516 KB Output is correct
162 Correct 27 ms 10324 KB Output is correct