Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 55d0e51

Browse files
Cacti problems
1 parent f254151 commit 55d0e51

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

‎DMOJ/coci07c1p6.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXN = 2*1e4 + 10;
5+
6+
vector<int> grafo[MAXN],tree[MAXN];
7+
deque<int> ciclo[MAXN];
8+
int cicloId,N,M,path[MAXN],tour[MAXN],dfs1Parent[MAXN],num[MAXN],low[MAXN],dfsCount;
9+
10+
void dfs1(int v,int p){
11+
12+
num[v] = ++dfsCount;
13+
low[v] = dfsCount;
14+
15+
16+
for(int u : grafo[v]){
17+
if(u == p) continue;
18+
if(num[u] == 0){
19+
dfs1Parent[u] = v;
20+
dfs1(u,v);
21+
if(num[v] < low[u]){
22+
tree[u].push_back(v);
23+
tree[v].push_back(u);
24+
}
25+
low[v] = min(low[v],low[u]);
26+
}
27+
else if(num[u] < num[v]){
28+
low[v] = min(low[v],num[u]);
29+
cicloId++;
30+
int k = v;
31+
while(k != u){
32+
ciclo[cicloId].push_back(k);
33+
k = dfs1Parent[k];
34+
}
35+
ciclo[cicloId].push_back(u);
36+
reverse(ciclo[cicloId].begin(),ciclo[cicloId].end());
37+
for(int k : ciclo[cicloId]){
38+
tree[k].push_back(cicloId);
39+
tree[cicloId].push_back(k);
40+
}
41+
}
42+
}
43+
44+
}
45+
46+
void dfs2(int v,int p){
47+
48+
if(v <= N){
49+
50+
tour[v] = 0;
51+
path[v] = 0;
52+
53+
for(int u : tree[v]){
54+
if(u == p) continue;
55+
dfs2(u,v);
56+
if(u > N){
57+
tour[v] += (1 + tour[u]);
58+
}
59+
}
60+
61+
for(int u : tree[v]){
62+
63+
if(u == p) continue;
64+
65+
if(u <= N){
66+
path[v] = max(path[v], tour[v] + path[u] + 1);
67+
}
68+
else{
69+
path[v] = max(path[v], tour[v] - (1 + tour[u]) + (1 + path[u]) );
70+
}
71+
72+
}
73+
74+
path[v] = max(path[v],tour[v]);
75+
76+
}
77+
else{
78+
79+
tour[v] = 0;
80+
path[v] = 0;
81+
82+
while(ciclo[v].front() != p){
83+
ciclo[v].push_back(ciclo[v].front());
84+
ciclo[v].pop_front();
85+
}
86+
87+
for(int u : ciclo[v]){
88+
if(u == p) continue;
89+
dfs2(u,v);
90+
tour[v] += (1 + tour[u]);
91+
}
92+
93+
int pref = 0;
94+
for(int u : ciclo[v]){
95+
if(u == p) continue;
96+
path[v] = max(path[v], pref + path[u] );
97+
pref += (1 + tour[u]);
98+
}
99+
100+
reverse(ciclo[v].begin(),ciclo[v].end());
101+
pref = 0;
102+
for(int u : ciclo[v]){
103+
if(u == p) continue;
104+
path[v] = max(path[v], pref + path[u] );
105+
pref += (1 + tour[u]);
106+
}
107+
108+
path[v] = max(path[v],tour[v]);
109+
110+
}
111+
112+
}
113+
114+
int main(){
115+
116+
scanf("%d %d",&N,&M);
117+
cicloId = N;
118+
119+
for(int i = 1;i<=M;i++){
120+
int u,v;
121+
scanf("%d %d",&u,&v);
122+
grafo[u].push_back(v);
123+
grafo[v].push_back(u);
124+
}
125+
126+
dfs1(1,-1);
127+
dfs2(1,-1);
128+
129+
//for(int i = 1;i<=cicloId;i++) printf("I %d P %d T %d\n",i,path[i],tour[i]);
130+
131+
printf("%d\n",path[1]);
132+
133+
return 0;
134+
135+
}

‎DMOJ/si17c3p4.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
6+
const int MAXN = 2*1e4 + 10;
7+
const int MOD = 1e9 + 7;
8+
9+
vector<int> grafo[MAXN];
10+
int num[MAXN],low[MAXN],dfsParent[MAXN],level[MAXN],dfsCount,went_up[MAXN],isCactus,N,M;
11+
ll ans;
12+
13+
void dfs(int v,int p){
14+
15+
if(!isCactus) return;
16+
17+
num[v] = ++dfsCount;
18+
low[v] = num[v];
19+
20+
for(int u : grafo[v]){
21+
if(u == p) continue;
22+
if(num[u] == 0){
23+
dfsParent[u] = v;
24+
level[u] = level[v] + 1;
25+
dfs(u,v);
26+
}
27+
else if(num[u] < num[v]){
28+
low[v] = min(low[v],num[u]);
29+
ans = (ans * (level[v] - level[u] + 1)) % MOD;
30+
int k = v;
31+
while(k != u){
32+
if(went_up[k]){
33+
isCactus = 0;
34+
break;
35+
}
36+
went_up[k] = 1;
37+
k = dfsParent[k];
38+
}
39+
}
40+
}
41+
42+
}
43+
44+
int main(){
45+
46+
ans = 1;
47+
isCactus = 1;
48+
scanf("%d %d",&N,&M);
49+
for(int i = 1;i<=M;i++){
50+
int u,v;
51+
scanf("%d %d",&u,&v);
52+
grafo[u].push_back(v);
53+
grafo[v].push_back(u);
54+
}
55+
56+
dfs(1,-1);
57+
58+
for(int i = 2;i<=N;i++){
59+
if(num[i] == 0) isCactus = 0;
60+
}
61+
62+
if(isCactus) printf("%lld\n",ans);
63+
else printf("safe\n");
64+
65+
return 0;
66+
}

‎DMOJ/tle17c4p5.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long ll;
5+
6+
const int MAXN = 200010;
7+
8+
ll seg[4*MAXN],MOD;
9+
int maior_primo[MAXN],frequencia[MAXN],N;
10+
11+
ll binary_expo(ll x,int y){
12+
ll ans = 1,sq = x;
13+
for(int i = 0;(1 << i) <= y;i++){
14+
if(y & (1 << i)) ans = (ans*sq) % MOD;
15+
sq = (sq*sq) % MOD;
16+
}
17+
return ans;
18+
}
19+
20+
void update(int pos,int left,int right,int x){
21+
if(left == right){
22+
seg[pos] = binary_expo(x,frequencia[x]);
23+
return;
24+
}
25+
int mid = (left+right)/2;
26+
if(x <= mid) update(2*pos,left,mid,x);
27+
else update(2*pos+1,mid+1,right,x);
28+
seg[pos] = (seg[2*pos]*seg[2*pos+1]) % MOD;
29+
}
30+
31+
void add(int x){
32+
while(x > 1){
33+
int y = maior_primo[x];
34+
frequencia[y]++;
35+
update(1,1,N,y);
36+
x /= y;
37+
}
38+
}
39+
40+
void remove(int x){
41+
while(x > 1){
42+
int y = maior_primo[x];
43+
frequencia[y]--;
44+
update(1,1,N,y);
45+
x /= y;
46+
}
47+
}
48+
49+
int main(){
50+
51+
scanf("%d %lld",&N,&MOD);
52+
53+
maior_primo[1] = 1;
54+
for(int i = 2;i <= N;i++){
55+
if(maior_primo[i] != 0) continue;
56+
for(int j = i;j<=N;j+=i) maior_primo[j] = i;
57+
}
58+
59+
for(int i = 1;i<=4*N;i++) seg[i] = 1;
60+
61+
printf("1\n");
62+
for(int i = N,j = 1;i>=1;i--,j++){
63+
add(i);
64+
remove(j);
65+
printf("%lld\n",seg[1]);
66+
}
67+
68+
return 0;
69+
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /