답안 #1003594

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003594 2024-06-20T13:30:59 Z aykhn Subtree (INOI20_subtree) C++17
100 / 100
45 ms 23636 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 2652 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2828 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2808 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2812 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 1 ms 2916 KB Output is correct
20 Correct 1 ms 2904 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 2 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2808 KB Output is correct
27 Correct 2 ms 2648 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 2648 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 2908 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 2 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 2 ms 2908 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2648 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 3 ms 3276 KB Output is correct
3 Correct 28 ms 9640 KB Output is correct
4 Correct 29 ms 9556 KB Output is correct
5 Correct 27 ms 9484 KB Output is correct
6 Correct 24 ms 9564 KB Output is correct
7 Correct 32 ms 20380 KB Output is correct
8 Correct 30 ms 16576 KB Output is correct
9 Correct 32 ms 16720 KB Output is correct
10 Correct 24 ms 10076 KB Output is correct
11 Correct 26 ms 9480 KB Output is correct
12 Correct 36 ms 9044 KB Output is correct
13 Correct 31 ms 9052 KB Output is correct
14 Correct 24 ms 14084 KB Output is correct
15 Correct 35 ms 22100 KB Output is correct
16 Correct 45 ms 23636 KB Output is correct
17 Correct 27 ms 9052 KB Output is correct
18 Correct 27 ms 8960 KB Output is correct
19 Correct 27 ms 9148 KB Output is correct
20 Correct 21 ms 9680 KB Output is correct
21 Correct 25 ms 9812 KB Output is correct
22 Correct 22 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2828 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2808 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2812 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 1 ms 2916 KB Output is correct
20 Correct 1 ms 2904 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 2 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2808 KB Output is correct
27 Correct 2 ms 2648 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 2648 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 2908 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 2 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 2 ms 2908 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2648 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 3 ms 3160 KB Output is correct
53 Correct 3 ms 3164 KB Output is correct
54 Correct 4 ms 3164 KB Output is correct
55 Correct 3 ms 3288 KB Output is correct
56 Correct 3 ms 3676 KB Output is correct
57 Correct 3 ms 3164 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 3160 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 2 ms 3164 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 2 ms 3164 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 3224 KB Output is correct
75 Correct 3 ms 3420 KB Output is correct
76 Correct 3 ms 3320 KB Output is correct
77 Correct 3 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 3 ms 3164 KB Output is correct
81 Correct 2 ms 3336 KB Output is correct
82 Correct 2 ms 3164 KB Output is correct
83 Correct 3 ms 3160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2828 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2808 KB Output is correct
11 Correct 1 ms 2648 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 1 ms 2812 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 1 ms 2916 KB Output is correct
20 Correct 1 ms 2904 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 2 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 2 ms 2808 KB Output is correct
27 Correct 2 ms 2648 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 2648 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 2908 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 1 ms 2652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 2 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 2 ms 2908 KB Output is correct
46 Correct 1 ms 2652 KB Output is correct
47 Correct 1 ms 2652 KB Output is correct
48 Correct 1 ms 2652 KB Output is correct
49 Correct 1 ms 2648 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 3 ms 3276 KB Output is correct
54 Correct 28 ms 9640 KB Output is correct
55 Correct 29 ms 9556 KB Output is correct
56 Correct 27 ms 9484 KB Output is correct
57 Correct 24 ms 9564 KB Output is correct
58 Correct 32 ms 20380 KB Output is correct
59 Correct 30 ms 16576 KB Output is correct
60 Correct 32 ms 16720 KB Output is correct
61 Correct 24 ms 10076 KB Output is correct
62 Correct 26 ms 9480 KB Output is correct
63 Correct 36 ms 9044 KB Output is correct
64 Correct 31 ms 9052 KB Output is correct
65 Correct 24 ms 14084 KB Output is correct
66 Correct 35 ms 22100 KB Output is correct
67 Correct 45 ms 23636 KB Output is correct
68 Correct 27 ms 9052 KB Output is correct
69 Correct 27 ms 8960 KB Output is correct
70 Correct 27 ms 9148 KB Output is correct
71 Correct 21 ms 9680 KB Output is correct
72 Correct 25 ms 9812 KB Output is correct
73 Correct 22 ms 9812 KB Output is correct
74 Correct 3 ms 3160 KB Output is correct
75 Correct 3 ms 3164 KB Output is correct
76 Correct 4 ms 3164 KB Output is correct
77 Correct 3 ms 3288 KB Output is correct
78 Correct 3 ms 3676 KB Output is correct
79 Correct 3 ms 3164 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 3160 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 2 ms 3164 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 2 ms 3164 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 3224 KB Output is correct
97 Correct 3 ms 3420 KB Output is correct
98 Correct 3 ms 3320 KB Output is correct
99 Correct 3 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 3 ms 3164 KB Output is correct
103 Correct 2 ms 3336 KB Output is correct
104 Correct 2 ms 3164 KB Output is correct
105 Correct 3 ms 3160 KB Output is correct
106 Correct 32 ms 11260 KB Output is correct
107 Correct 31 ms 11088 KB Output is correct
108 Correct 28 ms 11300 KB Output is correct
109 Correct 35 ms 11308 KB Output is correct
110 Correct 25 ms 10840 KB Output is correct
111 Correct 29 ms 13068 KB Output is correct
112 Correct 43 ms 20620 KB Output is correct
113 Correct 34 ms 10840 KB Output is correct
114 Correct 31 ms 11092 KB Output is correct
115 Correct 22 ms 9564 KB Output is correct
116 Correct 44 ms 21132 KB Output is correct
117 Correct 38 ms 19624 KB Output is correct
118 Correct 24 ms 12496 KB Output is correct
119 Correct 41 ms 10956 KB Output is correct
120 Correct 35 ms 11348 KB Output is correct
121 Correct 37 ms 11868 KB Output is correct
122 Correct 40 ms 11812 KB Output is correct
123 Correct 24 ms 10064 KB Output is correct
124 Correct 28 ms 10196 KB Output is correct
125 Correct 22 ms 9720 KB Output is correct
126 Correct 30 ms 11244 KB Output is correct
127 Correct 35 ms 11088 KB Output is correct
128 Correct 32 ms 11348 KB Output is correct
129 Correct 32 ms 11356 KB Output is correct
130 Correct 24 ms 10588 KB Output is correct
131 Correct 28 ms 12904 KB Output is correct
132 Correct 42 ms 20116 KB Output is correct
133 Correct 43 ms 10832 KB Output is correct
134 Correct 34 ms 11096 KB Output is correct
135 Correct 22 ms 9680 KB Output is correct
136 Correct 36 ms 18776 KB Output is correct
137 Correct 40 ms 19188 KB Output is correct
138 Correct 23 ms 12484 KB Output is correct
139 Correct 30 ms 10708 KB Output is correct
140 Correct 37 ms 11344 KB Output is correct
141 Correct 38 ms 11856 KB Output is correct
142 Correct 39 ms 11860 KB Output is correct
143 Correct 28 ms 10076 KB Output is correct
144 Correct 23 ms 9812 KB Output is correct
145 Correct 21 ms 9680 KB Output is correct
146 Correct 36 ms 11356 KB Output is correct
147 Correct 33 ms 10588 KB Output is correct
148 Correct 36 ms 11092 KB Output is correct
149 Correct 34 ms 12120 KB Output is correct
150 Correct 41 ms 22484 KB Output is correct
151 Correct 38 ms 11344 KB Output is correct
152 Correct 40 ms 12116 KB Output is correct
153 Correct 40 ms 12264 KB Output is correct
154 Correct 37 ms 12120 KB Output is correct
155 Correct 28 ms 11608 KB Output is correct
156 Correct 44 ms 19472 KB Output is correct
157 Correct 42 ms 18260 KB Output is correct
158 Correct 39 ms 17016 KB Output is correct
159 Correct 28 ms 10580 KB Output is correct
160 Correct 35 ms 20820 KB Output is correct
161 Correct 27 ms 10444 KB Output is correct
162 Correct 23 ms 11012 KB Output is correct