Submission #1003339

# Submission time Handle Problem Language Result Execution time Memory
1003339 2024-06-20T09:03:45 Z vjudge1 Subtree (INOI20_subtree) C++17
100 / 100
100 ms 36864 KB
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const long long mod = 1e9+7;
long long binpow(long long a, long long b, long long MOD=mod) {
  a%=MOD;
  long long res = 1;
  while(b) {
    if(b&1){ 
      res=(res*a)%MOD;
    }
    a=(a*a)%MOD;
    b>>=1ll;
  }
  res%=MOD;
  return res;
}
vector<int> g[N];
vector<int> bridges[N];
vector<int> notBridges[N];
int add[N], depth[N];
bool vis[N];
vector<vector<int>> cycles;
int getCycle[N];
void dfsAdd(int at, int p) {
  vis[at]=1;
  for(int to:g[at]) {
    if(to==p)continue;
    if(vis[to] == 1) 
    {
      if(depth[at] > depth[to]) {
        add[at]++;
        add[to]--;
        notBridges[to].push_back(at);
        notBridges[at].push_back(to);
      }
      continue;
    }
    depth[to]=depth[at]+1;
    dfsAdd(to, at);
  }
}
void dfsGetBridges(int at, int p) {
  vis[at]=1;
  for(int to:g[at]) {
    if(vis[to])continue;
    dfsGetBridges(to, at);
    add[at]+=add[to];
  }
  if(p==0)return;
  if(add[at] == 0) {
    bridges[at].push_back(p);
    bridges[p].push_back(at);
  }
  else {
    notBridges[at].push_back(p);
    notBridges[p].push_back(at);
  }
}
vector<int> tempcycle;
void dfsGetCycles(int at) {
  tempcycle.push_back(at);
  vis[at]=1;
  for(int to:notBridges[at]) {
    if(vis[to])continue;
    dfsGetCycles(to);
  }
}
long long dp[N], tmpdp[N];
long long ans=0;
void dfsCalc(int c, int cc) {
  vis[c]=1;
  for(int at:cycles[c]) {
    dp[at]=1;
    for(int to:g[at]) {
      if(!vis[getCycle[to]]) {
        dfsCalc(getCycle[to], to);
        dp[at] = (dp[at]*(dp[to]+1))%mod;
      }
    }
  }
  if(cycles[c].size()==1){
    ans+=dp[cycles[c][0]];
    ans%=mod;
    return;
  }
  if(cc == -1) {
    cc=cycles[c][0];
  }


  int idx=0;
  for(int i = 0;i<cycles[c].size();i++) {
    if(cycles[c][i] == cc ) {
      idx=i;
    }
  }
  vector<int> cycle;
  for(int i = idx+1;i<cycles[c].size();i++)cycle.push_back(cycles[c][i]);
  for(int i = 0;i<idx;i++)cycle.push_back(cycles[c][i]);
  long long prod = 1;
  long long divsum = 1;
  int ss = cycle.size();
  int pref[ss];
  for(int i = 0;i<cycle.size();i++) {
    prod*=dp[cycle[i]];
    prod%=mod;
    ans += (prod*divsum)%mod;
    divsum = (divsum + binpow(prod, mod-2))%mod;
    pref[i] = prod;
  }
  for(int i = 1;i<cycle.size();i++) {
    pref[i] = (pref[i-1]+pref[i])%mod;
  }
  divsum=0;
  prod = dp[cc];
  {
    int i = cycle.size();
    ans=(ans+prod)%mod;
    tmpdp[cc] = (tmpdp[cc]+prod)%mod;
    ans=(ans+(prod*(pref[i-2]))%mod)%mod;
    tmpdp[cc] = (tmpdp[cc]+prod*(pref[i-2]))%mod;
  }
  for(int i = cycle.size()-1;i>=0;i--){ 
    prod = (prod*dp[cycle[i]])%mod;
    if(i!=0) {
      ans=(ans+prod)%mod;
      tmpdp[cc] = (tmpdp[cc]+prod)%mod;
    }
    if(i >= 2) {
      ans=(ans+(prod*(pref[i-2]))%mod)%mod;
      tmpdp[cc] = (tmpdp[cc]+prod*(pref[i-2]))%mod;
    }
  }
  dp[cc] = tmpdp[cc];
}
int main () {
  cin.tie(0)->sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  for(int i = 0;i<m;i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfsAdd(1, 0);
  memset(vis, 0, sizeof vis);
  dfsGetBridges(1, 0);
  memset(vis, 0, sizeof vis);
  for(int i = 1;i<=n;i++) {
    if(vis[i] == 0) {
      dfsGetCycles(i);
      for(int c:tempcycle) {
        getCycle[c] = cycles.size();
      }
      cycles.push_back(tempcycle);
      tempcycle.clear();
    }
  }
  memset(vis, 0, sizeof vis);
  dfsCalc(0, -1);
  cout << ans%mod << "\n";
}

Compilation message

Main.cpp: In function 'void dfsCalc(int, int)':
Main.cpp:93:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |   for(int i = 0;i<cycles[c].size();i++) {
      |                 ~^~~~~~~~~~~~~~~~~
Main.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |   for(int i = idx+1;i<cycles[c].size();i++)cycle.push_back(cycles[c][i]);
      |                     ~^~~~~~~~~~~~~~~~~
Main.cpp:105:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |   for(int i = 0;i<cycle.size();i++) {
      |                 ~^~~~~~~~~~~~~
Main.cpp:112:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |   for(int i = 1;i<cycle.size();i++) {
      |                 ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7512 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7396 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 4 ms 7528 KB Output is correct
16 Correct 4 ms 7516 KB Output is correct
17 Correct 5 ms 7516 KB Output is correct
18 Correct 4 ms 7516 KB Output is correct
19 Correct 3 ms 7512 KB Output is correct
20 Correct 3 ms 7516 KB Output is correct
21 Correct 3 ms 7512 KB Output is correct
22 Correct 3 ms 7516 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7516 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 3 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
30 Correct 3 ms 7516 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7404 KB Output is correct
33 Correct 4 ms 7516 KB Output is correct
34 Correct 2 ms 7516 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
36 Correct 3 ms 7516 KB Output is correct
37 Correct 3 ms 7516 KB Output is correct
38 Correct 3 ms 7532 KB Output is correct
39 Correct 5 ms 7516 KB Output is correct
40 Correct 3 ms 7628 KB Output is correct
41 Correct 3 ms 7516 KB Output is correct
42 Correct 3 ms 7516 KB Output is correct
43 Correct 4 ms 7516 KB Output is correct
44 Correct 2 ms 7628 KB Output is correct
45 Correct 3 ms 7516 KB Output is correct
46 Correct 4 ms 7472 KB Output is correct
47 Correct 3 ms 7516 KB Output is correct
48 Correct 3 ms 7516 KB Output is correct
49 Correct 4 ms 7612 KB Output is correct
50 Correct 3 ms 7516 KB Output is correct
51 Correct 3 ms 7772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7516 KB Output is correct
2 Correct 9 ms 9108 KB Output is correct
3 Correct 70 ms 22640 KB Output is correct
4 Correct 58 ms 22532 KB Output is correct
5 Correct 58 ms 22532 KB Output is correct
6 Correct 56 ms 22660 KB Output is correct
7 Correct 72 ms 33540 KB Output is correct
8 Correct 70 ms 29568 KB Output is correct
9 Correct 66 ms 29440 KB Output is correct
10 Correct 46 ms 23296 KB Output is correct
11 Correct 51 ms 22852 KB Output is correct
12 Correct 78 ms 22400 KB Output is correct
13 Correct 60 ms 22276 KB Output is correct
14 Correct 60 ms 27668 KB Output is correct
15 Correct 72 ms 35324 KB Output is correct
16 Correct 85 ms 36864 KB Output is correct
17 Correct 60 ms 22532 KB Output is correct
18 Correct 57 ms 22532 KB Output is correct
19 Correct 56 ms 22516 KB Output is correct
20 Correct 41 ms 22596 KB Output is correct
21 Correct 41 ms 22800 KB Output is correct
22 Correct 41 ms 22716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7512 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7396 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 4 ms 7528 KB Output is correct
16 Correct 4 ms 7516 KB Output is correct
17 Correct 5 ms 7516 KB Output is correct
18 Correct 4 ms 7516 KB Output is correct
19 Correct 3 ms 7512 KB Output is correct
20 Correct 3 ms 7516 KB Output is correct
21 Correct 3 ms 7512 KB Output is correct
22 Correct 3 ms 7516 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7516 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 3 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
30 Correct 3 ms 7516 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7404 KB Output is correct
33 Correct 4 ms 7516 KB Output is correct
34 Correct 2 ms 7516 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
36 Correct 3 ms 7516 KB Output is correct
37 Correct 3 ms 7516 KB Output is correct
38 Correct 3 ms 7532 KB Output is correct
39 Correct 5 ms 7516 KB Output is correct
40 Correct 3 ms 7628 KB Output is correct
41 Correct 3 ms 7516 KB Output is correct
42 Correct 3 ms 7516 KB Output is correct
43 Correct 4 ms 7516 KB Output is correct
44 Correct 2 ms 7628 KB Output is correct
45 Correct 3 ms 7516 KB Output is correct
46 Correct 4 ms 7472 KB Output is correct
47 Correct 3 ms 7516 KB Output is correct
48 Correct 3 ms 7516 KB Output is correct
49 Correct 4 ms 7612 KB Output is correct
50 Correct 3 ms 7516 KB Output is correct
51 Correct 3 ms 7772 KB Output is correct
52 Correct 5 ms 8284 KB Output is correct
53 Correct 5 ms 8420 KB Output is correct
54 Correct 5 ms 8284 KB Output is correct
55 Correct 6 ms 8536 KB Output is correct
56 Correct 7 ms 8536 KB Output is correct
57 Correct 5 ms 8324 KB Output is correct
58 Correct 5 ms 8284 KB Output is correct
59 Correct 5 ms 8524 KB Output is correct
60 Correct 5 ms 8284 KB Output is correct
61 Correct 5 ms 8284 KB Output is correct
62 Correct 6 ms 8284 KB Output is correct
63 Correct 5 ms 8284 KB Output is correct
64 Correct 5 ms 8404 KB Output is correct
65 Correct 5 ms 8284 KB Output is correct
66 Correct 5 ms 8284 KB Output is correct
67 Correct 5 ms 8536 KB Output is correct
68 Correct 6 ms 8540 KB Output is correct
69 Correct 5 ms 8284 KB Output is correct
70 Correct 5 ms 8348 KB Output is correct
71 Correct 6 ms 8300 KB Output is correct
72 Correct 6 ms 8284 KB Output is correct
73 Correct 5 ms 8280 KB Output is correct
74 Correct 6 ms 8004 KB Output is correct
75 Correct 6 ms 8344 KB Output is correct
76 Correct 6 ms 8284 KB Output is correct
77 Correct 6 ms 8540 KB Output is correct
78 Correct 6 ms 8284 KB Output is correct
79 Correct 6 ms 8284 KB Output is correct
80 Correct 5 ms 8220 KB Output is correct
81 Correct 5 ms 8284 KB Output is correct
82 Correct 6 ms 8280 KB Output is correct
83 Correct 5 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7512 KB Output is correct
6 Correct 3 ms 7516 KB Output is correct
7 Correct 3 ms 7396 KB Output is correct
8 Correct 3 ms 7516 KB Output is correct
9 Correct 3 ms 7516 KB Output is correct
10 Correct 3 ms 7516 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 3 ms 7516 KB Output is correct
14 Correct 3 ms 7516 KB Output is correct
15 Correct 4 ms 7528 KB Output is correct
16 Correct 4 ms 7516 KB Output is correct
17 Correct 5 ms 7516 KB Output is correct
18 Correct 4 ms 7516 KB Output is correct
19 Correct 3 ms 7512 KB Output is correct
20 Correct 3 ms 7516 KB Output is correct
21 Correct 3 ms 7512 KB Output is correct
22 Correct 3 ms 7516 KB Output is correct
23 Correct 3 ms 7516 KB Output is correct
24 Correct 3 ms 7516 KB Output is correct
25 Correct 3 ms 7516 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 3 ms 7516 KB Output is correct
28 Correct 3 ms 7516 KB Output is correct
29 Correct 3 ms 7516 KB Output is correct
30 Correct 3 ms 7516 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7404 KB Output is correct
33 Correct 4 ms 7516 KB Output is correct
34 Correct 2 ms 7516 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
36 Correct 3 ms 7516 KB Output is correct
37 Correct 3 ms 7516 KB Output is correct
38 Correct 3 ms 7532 KB Output is correct
39 Correct 5 ms 7516 KB Output is correct
40 Correct 3 ms 7628 KB Output is correct
41 Correct 3 ms 7516 KB Output is correct
42 Correct 3 ms 7516 KB Output is correct
43 Correct 4 ms 7516 KB Output is correct
44 Correct 2 ms 7628 KB Output is correct
45 Correct 3 ms 7516 KB Output is correct
46 Correct 4 ms 7472 KB Output is correct
47 Correct 3 ms 7516 KB Output is correct
48 Correct 3 ms 7516 KB Output is correct
49 Correct 4 ms 7612 KB Output is correct
50 Correct 3 ms 7516 KB Output is correct
51 Correct 3 ms 7772 KB Output is correct
52 Correct 3 ms 7516 KB Output is correct
53 Correct 9 ms 9108 KB Output is correct
54 Correct 70 ms 22640 KB Output is correct
55 Correct 58 ms 22532 KB Output is correct
56 Correct 58 ms 22532 KB Output is correct
57 Correct 56 ms 22660 KB Output is correct
58 Correct 72 ms 33540 KB Output is correct
59 Correct 70 ms 29568 KB Output is correct
60 Correct 66 ms 29440 KB Output is correct
61 Correct 46 ms 23296 KB Output is correct
62 Correct 51 ms 22852 KB Output is correct
63 Correct 78 ms 22400 KB Output is correct
64 Correct 60 ms 22276 KB Output is correct
65 Correct 60 ms 27668 KB Output is correct
66 Correct 72 ms 35324 KB Output is correct
67 Correct 85 ms 36864 KB Output is correct
68 Correct 60 ms 22532 KB Output is correct
69 Correct 57 ms 22532 KB Output is correct
70 Correct 56 ms 22516 KB Output is correct
71 Correct 41 ms 22596 KB Output is correct
72 Correct 41 ms 22800 KB Output is correct
73 Correct 41 ms 22716 KB Output is correct
74 Correct 5 ms 8284 KB Output is correct
75 Correct 5 ms 8420 KB Output is correct
76 Correct 5 ms 8284 KB Output is correct
77 Correct 6 ms 8536 KB Output is correct
78 Correct 7 ms 8536 KB Output is correct
79 Correct 5 ms 8324 KB Output is correct
80 Correct 5 ms 8284 KB Output is correct
81 Correct 5 ms 8524 KB Output is correct
82 Correct 5 ms 8284 KB Output is correct
83 Correct 5 ms 8284 KB Output is correct
84 Correct 6 ms 8284 KB Output is correct
85 Correct 5 ms 8284 KB Output is correct
86 Correct 5 ms 8404 KB Output is correct
87 Correct 5 ms 8284 KB Output is correct
88 Correct 5 ms 8284 KB Output is correct
89 Correct 5 ms 8536 KB Output is correct
90 Correct 6 ms 8540 KB Output is correct
91 Correct 5 ms 8284 KB Output is correct
92 Correct 5 ms 8348 KB Output is correct
93 Correct 6 ms 8300 KB Output is correct
94 Correct 6 ms 8284 KB Output is correct
95 Correct 5 ms 8280 KB Output is correct
96 Correct 6 ms 8004 KB Output is correct
97 Correct 6 ms 8344 KB Output is correct
98 Correct 6 ms 8284 KB Output is correct
99 Correct 6 ms 8540 KB Output is correct
100 Correct 6 ms 8284 KB Output is correct
101 Correct 6 ms 8284 KB Output is correct
102 Correct 5 ms 8220 KB Output is correct
103 Correct 5 ms 8284 KB Output is correct
104 Correct 6 ms 8280 KB Output is correct
105 Correct 5 ms 8536 KB Output is correct
106 Correct 83 ms 23044 KB Output is correct
107 Correct 65 ms 23040 KB Output is correct
108 Correct 65 ms 22796 KB Output is correct
109 Correct 71 ms 22788 KB Output is correct
110 Correct 64 ms 23476 KB Output is correct
111 Correct 57 ms 24576 KB Output is correct
112 Correct 95 ms 26884 KB Output is correct
113 Correct 68 ms 20980 KB Output is correct
114 Correct 85 ms 22200 KB Output is correct
115 Correct 43 ms 23108 KB Output is correct
116 Correct 74 ms 27392 KB Output is correct
117 Correct 80 ms 26372 KB Output is correct
118 Correct 51 ms 24332 KB Output is correct
119 Correct 84 ms 21000 KB Output is correct
120 Correct 76 ms 21000 KB Output is correct
121 Correct 75 ms 21348 KB Output is correct
122 Correct 78 ms 21512 KB Output is correct
123 Correct 43 ms 23300 KB Output is correct
124 Correct 50 ms 23040 KB Output is correct
125 Correct 55 ms 22848 KB Output is correct
126 Correct 67 ms 23040 KB Output is correct
127 Correct 67 ms 23056 KB Output is correct
128 Correct 62 ms 22892 KB Output is correct
129 Correct 68 ms 22788 KB Output is correct
130 Correct 54 ms 23420 KB Output is correct
131 Correct 60 ms 24572 KB Output is correct
132 Correct 79 ms 26672 KB Output is correct
133 Correct 69 ms 20856 KB Output is correct
134 Correct 70 ms 22188 KB Output is correct
135 Correct 41 ms 23128 KB Output is correct
136 Correct 76 ms 25688 KB Output is correct
137 Correct 81 ms 26116 KB Output is correct
138 Correct 44 ms 24388 KB Output is correct
139 Correct 68 ms 21000 KB Output is correct
140 Correct 86 ms 21000 KB Output is correct
141 Correct 90 ms 21256 KB Output is correct
142 Correct 75 ms 21480 KB Output is correct
143 Correct 47 ms 23300 KB Output is correct
144 Correct 45 ms 23044 KB Output is correct
145 Correct 51 ms 22928 KB Output is correct
146 Correct 78 ms 19508 KB Output is correct
147 Correct 61 ms 18256 KB Output is correct
148 Correct 67 ms 18256 KB Output is correct
149 Correct 69 ms 19028 KB Output is correct
150 Correct 72 ms 26700 KB Output is correct
151 Correct 68 ms 19344 KB Output is correct
152 Correct 89 ms 21104 KB Output is correct
153 Correct 82 ms 21004 KB Output is correct
154 Correct 82 ms 20676 KB Output is correct
155 Correct 64 ms 23808 KB Output is correct
156 Correct 71 ms 23052 KB Output is correct
157 Correct 75 ms 22356 KB Output is correct
158 Correct 78 ms 21840 KB Output is correct
159 Correct 87 ms 23040 KB Output is correct
160 Correct 100 ms 31840 KB Output is correct
161 Correct 83 ms 22096 KB Output is correct
162 Correct 74 ms 23044 KB Output is correct