답안 #1003155

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003155 2024-06-20T06:57:08 Z vjudge1 Subtree (INOI20_subtree) C++17
100 / 100
116 ms 74320 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
 
const int MXN = 1e6 + 5;
const int LOG = 20;
const int mod = 1e9 + 7;

vector<int> adj[MXN];
vector<array<int, 2>> g[MXN];
int p[MXN], used[MXN], down[MXN], bak[MXN];
int in[MXN], t[MXN], tim;
int dp[MXN];

void _init(int a)
{
  t[a] = ++tim;
  used[a] = 1;
  for (array<int, 2> &v : g[a])
  {
    if (v[0] != p[a] && used[v[0]]) bak[v[1]] = 1;
  }
  for (array<int, 2> &v : g[a])
  {
    if (!used[v[0]]) 
    {
      p[v[0]] = a;
      _init(v[0]);
    }
  }
}

void dfs(int a)
{
  for (int &v : adj[a])
  {
    dfs(v);
  }
  if (down[a])
  {
    vector<int> path;
    int x = down[a];
    while (x)
    {
      path.push_back(x);
      in[x] = 1;
      if (x == a) break;
      else assert(!down[x]);
      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;
    g[u].push_back({v, i});
    g[v].push_back({u, i});
  }
  _init(1);
  for (int a = 1; a <= n; a++)
  {
    for (array<int, 2> &v : g[a])
    {
      if (t[v[0]] > t[a] && !bak[v[1]]) adj[a].push_back(v[0]);
      else if (t[v[0]] > t[a] && bak[v[1]]) down[a] = v[0];
    }
  }
  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:70: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]
   70 |       sum[i] = ((i + 1 < path.size() ? sum[i + 1] : 0) + co) % mod;
      |                  ~~~~~~^~~~~~~~~~~~~
Main.cpp:73: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]
   73 |     for (int i = 0; i + 1 < path.size(); i++)
      |                     ~~~~~~^~~~~~~~~~~~~
Main.cpp:83: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]
   83 |       if (i + 2 < path.size()) dp[a] = (dp[a] + ((sum[i + 2] * co) % mod)) % mod;
      |           ~~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 47452 KB Output is correct
2 Correct 17 ms 47424 KB Output is correct
3 Correct 20 ms 47448 KB Output is correct
4 Correct 18 ms 47400 KB Output is correct
5 Correct 22 ms 47448 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 21 ms 47448 KB Output is correct
8 Correct 18 ms 47448 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 18 ms 47452 KB Output is correct
11 Correct 19 ms 47452 KB Output is correct
12 Correct 20 ms 47452 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 18 ms 47452 KB Output is correct
15 Correct 18 ms 47428 KB Output is correct
16 Correct 18 ms 47448 KB Output is correct
17 Correct 18 ms 47452 KB Output is correct
18 Correct 19 ms 47452 KB Output is correct
19 Correct 17 ms 47452 KB Output is correct
20 Correct 17 ms 47300 KB Output is correct
21 Correct 18 ms 47448 KB Output is correct
22 Correct 18 ms 47452 KB Output is correct
23 Correct 22 ms 47452 KB Output is correct
24 Correct 18 ms 47240 KB Output is correct
25 Correct 19 ms 47452 KB Output is correct
26 Correct 18 ms 47452 KB Output is correct
27 Correct 18 ms 47388 KB Output is correct
28 Correct 18 ms 47448 KB Output is correct
29 Correct 18 ms 47452 KB Output is correct
30 Correct 17 ms 47364 KB Output is correct
31 Correct 18 ms 47448 KB Output is correct
32 Correct 18 ms 47296 KB Output is correct
33 Correct 18 ms 47468 KB Output is correct
34 Correct 18 ms 47452 KB Output is correct
35 Correct 22 ms 47452 KB Output is correct
36 Correct 18 ms 47452 KB Output is correct
37 Correct 20 ms 47452 KB Output is correct
38 Correct 17 ms 47452 KB Output is correct
39 Correct 17 ms 47452 KB Output is correct
40 Correct 18 ms 47452 KB Output is correct
41 Correct 18 ms 47440 KB Output is correct
42 Correct 18 ms 47452 KB Output is correct
43 Correct 18 ms 47240 KB Output is correct
44 Correct 22 ms 47452 KB Output is correct
45 Correct 18 ms 47452 KB Output is correct
46 Correct 23 ms 47452 KB Output is correct
47 Correct 18 ms 47452 KB Output is correct
48 Correct 20 ms 47452 KB Output is correct
49 Correct 17 ms 47248 KB Output is correct
50 Correct 19 ms 47452 KB Output is correct
51 Correct 19 ms 47268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 47452 KB Output is correct
2 Correct 22 ms 48476 KB Output is correct
3 Correct 59 ms 58548 KB Output is correct
4 Correct 58 ms 58520 KB Output is correct
5 Correct 62 ms 58708 KB Output is correct
6 Correct 56 ms 58656 KB Output is correct
7 Correct 68 ms 70740 KB Output is correct
8 Correct 73 ms 66132 KB Output is correct
9 Correct 67 ms 65640 KB Output is correct
10 Correct 59 ms 57940 KB Output is correct
11 Correct 56 ms 58328 KB Output is correct
12 Correct 68 ms 59332 KB Output is correct
13 Correct 72 ms 59792 KB Output is correct
14 Correct 62 ms 62916 KB Output is correct
15 Correct 76 ms 72392 KB Output is correct
16 Correct 75 ms 74320 KB Output is correct
17 Correct 75 ms 59728 KB Output is correct
18 Correct 74 ms 59924 KB Output is correct
19 Correct 65 ms 59720 KB Output is correct
20 Correct 44 ms 57772 KB Output is correct
21 Correct 51 ms 57392 KB Output is correct
22 Correct 49 ms 57212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 47452 KB Output is correct
2 Correct 17 ms 47424 KB Output is correct
3 Correct 20 ms 47448 KB Output is correct
4 Correct 18 ms 47400 KB Output is correct
5 Correct 22 ms 47448 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 21 ms 47448 KB Output is correct
8 Correct 18 ms 47448 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 18 ms 47452 KB Output is correct
11 Correct 19 ms 47452 KB Output is correct
12 Correct 20 ms 47452 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 18 ms 47452 KB Output is correct
15 Correct 18 ms 47428 KB Output is correct
16 Correct 18 ms 47448 KB Output is correct
17 Correct 18 ms 47452 KB Output is correct
18 Correct 19 ms 47452 KB Output is correct
19 Correct 17 ms 47452 KB Output is correct
20 Correct 17 ms 47300 KB Output is correct
21 Correct 18 ms 47448 KB Output is correct
22 Correct 18 ms 47452 KB Output is correct
23 Correct 22 ms 47452 KB Output is correct
24 Correct 18 ms 47240 KB Output is correct
25 Correct 19 ms 47452 KB Output is correct
26 Correct 18 ms 47452 KB Output is correct
27 Correct 18 ms 47388 KB Output is correct
28 Correct 18 ms 47448 KB Output is correct
29 Correct 18 ms 47452 KB Output is correct
30 Correct 17 ms 47364 KB Output is correct
31 Correct 18 ms 47448 KB Output is correct
32 Correct 18 ms 47296 KB Output is correct
33 Correct 18 ms 47468 KB Output is correct
34 Correct 18 ms 47452 KB Output is correct
35 Correct 22 ms 47452 KB Output is correct
36 Correct 18 ms 47452 KB Output is correct
37 Correct 20 ms 47452 KB Output is correct
38 Correct 17 ms 47452 KB Output is correct
39 Correct 17 ms 47452 KB Output is correct
40 Correct 18 ms 47452 KB Output is correct
41 Correct 18 ms 47440 KB Output is correct
42 Correct 18 ms 47452 KB Output is correct
43 Correct 18 ms 47240 KB Output is correct
44 Correct 22 ms 47452 KB Output is correct
45 Correct 18 ms 47452 KB Output is correct
46 Correct 23 ms 47452 KB Output is correct
47 Correct 18 ms 47452 KB Output is correct
48 Correct 20 ms 47452 KB Output is correct
49 Correct 17 ms 47248 KB Output is correct
50 Correct 19 ms 47452 KB Output is correct
51 Correct 19 ms 47268 KB Output is correct
52 Correct 20 ms 48068 KB Output is correct
53 Correct 19 ms 48148 KB Output is correct
54 Correct 20 ms 48216 KB Output is correct
55 Correct 21 ms 47964 KB Output is correct
56 Correct 19 ms 48828 KB Output is correct
57 Correct 20 ms 47952 KB Output is correct
58 Correct 23 ms 47964 KB Output is correct
59 Correct 19 ms 48304 KB Output is correct
60 Correct 19 ms 48060 KB Output is correct
61 Correct 20 ms 47956 KB Output is correct
62 Correct 20 ms 48220 KB Output is correct
63 Correct 21 ms 47952 KB Output is correct
64 Correct 19 ms 47964 KB Output is correct
65 Correct 21 ms 47912 KB Output is correct
66 Correct 20 ms 47964 KB Output is correct
67 Correct 19 ms 47960 KB Output is correct
68 Correct 19 ms 48476 KB Output is correct
69 Correct 20 ms 47964 KB Output is correct
70 Correct 20 ms 47960 KB Output is correct
71 Correct 21 ms 48028 KB Output is correct
72 Correct 19 ms 48168 KB Output is correct
73 Correct 20 ms 47960 KB Output is correct
74 Correct 21 ms 48188 KB Output is correct
75 Correct 20 ms 48436 KB Output is correct
76 Correct 20 ms 48104 KB Output is correct
77 Correct 21 ms 48728 KB Output is correct
78 Correct 20 ms 48220 KB Output is correct
79 Correct 20 ms 48472 KB Output is correct
80 Correct 20 ms 48220 KB Output is correct
81 Correct 19 ms 48084 KB Output is correct
82 Correct 20 ms 48116 KB Output is correct
83 Correct 19 ms 48040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 47452 KB Output is correct
2 Correct 17 ms 47424 KB Output is correct
3 Correct 20 ms 47448 KB Output is correct
4 Correct 18 ms 47400 KB Output is correct
5 Correct 22 ms 47448 KB Output is correct
6 Correct 18 ms 47452 KB Output is correct
7 Correct 21 ms 47448 KB Output is correct
8 Correct 18 ms 47448 KB Output is correct
9 Correct 18 ms 47448 KB Output is correct
10 Correct 18 ms 47452 KB Output is correct
11 Correct 19 ms 47452 KB Output is correct
12 Correct 20 ms 47452 KB Output is correct
13 Correct 18 ms 47452 KB Output is correct
14 Correct 18 ms 47452 KB Output is correct
15 Correct 18 ms 47428 KB Output is correct
16 Correct 18 ms 47448 KB Output is correct
17 Correct 18 ms 47452 KB Output is correct
18 Correct 19 ms 47452 KB Output is correct
19 Correct 17 ms 47452 KB Output is correct
20 Correct 17 ms 47300 KB Output is correct
21 Correct 18 ms 47448 KB Output is correct
22 Correct 18 ms 47452 KB Output is correct
23 Correct 22 ms 47452 KB Output is correct
24 Correct 18 ms 47240 KB Output is correct
25 Correct 19 ms 47452 KB Output is correct
26 Correct 18 ms 47452 KB Output is correct
27 Correct 18 ms 47388 KB Output is correct
28 Correct 18 ms 47448 KB Output is correct
29 Correct 18 ms 47452 KB Output is correct
30 Correct 17 ms 47364 KB Output is correct
31 Correct 18 ms 47448 KB Output is correct
32 Correct 18 ms 47296 KB Output is correct
33 Correct 18 ms 47468 KB Output is correct
34 Correct 18 ms 47452 KB Output is correct
35 Correct 22 ms 47452 KB Output is correct
36 Correct 18 ms 47452 KB Output is correct
37 Correct 20 ms 47452 KB Output is correct
38 Correct 17 ms 47452 KB Output is correct
39 Correct 17 ms 47452 KB Output is correct
40 Correct 18 ms 47452 KB Output is correct
41 Correct 18 ms 47440 KB Output is correct
42 Correct 18 ms 47452 KB Output is correct
43 Correct 18 ms 47240 KB Output is correct
44 Correct 22 ms 47452 KB Output is correct
45 Correct 18 ms 47452 KB Output is correct
46 Correct 23 ms 47452 KB Output is correct
47 Correct 18 ms 47452 KB Output is correct
48 Correct 20 ms 47452 KB Output is correct
49 Correct 17 ms 47248 KB Output is correct
50 Correct 19 ms 47452 KB Output is correct
51 Correct 19 ms 47268 KB Output is correct
52 Correct 18 ms 47452 KB Output is correct
53 Correct 22 ms 48476 KB Output is correct
54 Correct 59 ms 58548 KB Output is correct
55 Correct 58 ms 58520 KB Output is correct
56 Correct 62 ms 58708 KB Output is correct
57 Correct 56 ms 58656 KB Output is correct
58 Correct 68 ms 70740 KB Output is correct
59 Correct 73 ms 66132 KB Output is correct
60 Correct 67 ms 65640 KB Output is correct
61 Correct 59 ms 57940 KB Output is correct
62 Correct 56 ms 58328 KB Output is correct
63 Correct 68 ms 59332 KB Output is correct
64 Correct 72 ms 59792 KB Output is correct
65 Correct 62 ms 62916 KB Output is correct
66 Correct 76 ms 72392 KB Output is correct
67 Correct 75 ms 74320 KB Output is correct
68 Correct 75 ms 59728 KB Output is correct
69 Correct 74 ms 59924 KB Output is correct
70 Correct 65 ms 59720 KB Output is correct
71 Correct 44 ms 57772 KB Output is correct
72 Correct 51 ms 57392 KB Output is correct
73 Correct 49 ms 57212 KB Output is correct
74 Correct 20 ms 48068 KB Output is correct
75 Correct 19 ms 48148 KB Output is correct
76 Correct 20 ms 48216 KB Output is correct
77 Correct 21 ms 47964 KB Output is correct
78 Correct 19 ms 48828 KB Output is correct
79 Correct 20 ms 47952 KB Output is correct
80 Correct 23 ms 47964 KB Output is correct
81 Correct 19 ms 48304 KB Output is correct
82 Correct 19 ms 48060 KB Output is correct
83 Correct 20 ms 47956 KB Output is correct
84 Correct 20 ms 48220 KB Output is correct
85 Correct 21 ms 47952 KB Output is correct
86 Correct 19 ms 47964 KB Output is correct
87 Correct 21 ms 47912 KB Output is correct
88 Correct 20 ms 47964 KB Output is correct
89 Correct 19 ms 47960 KB Output is correct
90 Correct 19 ms 48476 KB Output is correct
91 Correct 20 ms 47964 KB Output is correct
92 Correct 20 ms 47960 KB Output is correct
93 Correct 21 ms 48028 KB Output is correct
94 Correct 19 ms 48168 KB Output is correct
95 Correct 20 ms 47960 KB Output is correct
96 Correct 21 ms 48188 KB Output is correct
97 Correct 20 ms 48436 KB Output is correct
98 Correct 20 ms 48104 KB Output is correct
99 Correct 21 ms 48728 KB Output is correct
100 Correct 20 ms 48220 KB Output is correct
101 Correct 20 ms 48472 KB Output is correct
102 Correct 20 ms 48220 KB Output is correct
103 Correct 19 ms 48084 KB Output is correct
104 Correct 20 ms 48116 KB Output is correct
105 Correct 19 ms 48040 KB Output is correct
106 Correct 60 ms 61268 KB Output is correct
107 Correct 67 ms 61520 KB Output is correct
108 Correct 63 ms 61520 KB Output is correct
109 Correct 65 ms 61556 KB Output is correct
110 Correct 49 ms 58192 KB Output is correct
111 Correct 55 ms 62036 KB Output is correct
112 Correct 91 ms 71728 KB Output is correct
113 Correct 71 ms 62548 KB Output is correct
114 Correct 81 ms 62412 KB Output is correct
115 Correct 51 ms 58340 KB Output is correct
116 Correct 81 ms 72784 KB Output is correct
117 Correct 116 ms 71376 KB Output is correct
118 Correct 51 ms 61892 KB Output is correct
119 Correct 77 ms 62556 KB Output is correct
120 Correct 84 ms 62800 KB Output is correct
121 Correct 87 ms 63312 KB Output is correct
122 Correct 106 ms 63400 KB Output is correct
123 Correct 49 ms 57936 KB Output is correct
124 Correct 60 ms 57392 KB Output is correct
125 Correct 46 ms 58052 KB Output is correct
126 Correct 51 ms 61132 KB Output is correct
127 Correct 62 ms 61472 KB Output is correct
128 Correct 71 ms 61520 KB Output is correct
129 Correct 75 ms 61620 KB Output is correct
130 Correct 52 ms 58160 KB Output is correct
131 Correct 53 ms 62032 KB Output is correct
132 Correct 88 ms 71248 KB Output is correct
133 Correct 73 ms 62608 KB Output is correct
134 Correct 71 ms 62472 KB Output is correct
135 Correct 48 ms 58300 KB Output is correct
136 Correct 70 ms 70512 KB Output is correct
137 Correct 77 ms 70560 KB Output is correct
138 Correct 62 ms 61560 KB Output is correct
139 Correct 76 ms 62544 KB Output is correct
140 Correct 75 ms 62808 KB Output is correct
141 Correct 71 ms 63224 KB Output is correct
142 Correct 76 ms 63312 KB Output is correct
143 Correct 50 ms 57940 KB Output is correct
144 Correct 58 ms 57524 KB Output is correct
145 Correct 46 ms 58052 KB Output is correct
146 Correct 73 ms 62840 KB Output is correct
147 Correct 76 ms 62288 KB Output is correct
148 Correct 67 ms 62040 KB Output is correct
149 Correct 69 ms 62944 KB Output is correct
150 Correct 75 ms 72904 KB Output is correct
151 Correct 77 ms 63068 KB Output is correct
152 Correct 87 ms 63568 KB Output is correct
153 Correct 76 ms 63784 KB Output is correct
154 Correct 66 ms 62424 KB Output is correct
155 Correct 50 ms 59832 KB Output is correct
156 Correct 80 ms 70268 KB Output is correct
157 Correct 70 ms 68944 KB Output is correct
158 Correct 71 ms 67820 KB Output is correct
159 Correct 63 ms 61996 KB Output is correct
160 Correct 96 ms 71468 KB Output is correct
161 Correct 68 ms 61012 KB Output is correct
162 Correct 48 ms 58964 KB Output is correct