0% found this document useful (0 votes)
23 views

Day 1 - 2

Uploaded by

ntlinh186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Day 1 - 2

Uploaded by

ntlinh186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

05-10 (BFS)

A. Sơ tán [EVA]
time limit per test
0.3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
10
2
10 8
9
6 7
7 5
5 8
8 1
1 10
10 3
3 4
4 9
9 2
output
Copy
1 4 1 2 1 3 2 0 3 0
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define ill pair<ll,ll>
9. #define F first
10. #define S second
11. #define pb push_back
12. #define lg2(x) 31-__builtin_clz(x)
13. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
14. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
15. using namespace std;
16. const int N=1e5+5;
17. const ll inf=1e18;
18. int n, m, k;
19. bool use[N];
20. ll d[N];
21. vector<int> ke[N], exitroom;
22. queue<int> qu;
23. void bfs() {
24. rep(i,1,n) d[i] = inf;
25. for(int u:exitroom) {
26. d[u] = 0;
27. qu.push(u);
28. }
29. while(!qu.empty()) {
30. int u = qu.front();
31. qu.pop();
32. for(int v:ke[u]) if(d[v] > d[u]+1) {
33. qu.push(v);
34. d[v] = d[u]+1;
35. }
36. }
37. }
38. signed main() { ///thieu bien
39. file("task");
40. speedup;
41. cin >> n;
42. cin >> k;
43. rep(i,1,k) {
44. int u;
45. cin >> u;
46. exitroom.pb(u);
47. }
48. cin >> m;
49. while(m--) {
50. int u, v;
51. cin >> u >> v;
52. ke[u].pb(v);
53. ke[v].pb(u);
54. }
55. bfs();
56. rep(i,1,n) cout << d[i] << ' ';
57. return 0;
58. }

B. Đảo ngược cạnh [REVERSE]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Example
input
Copy
7 7
1 2
3 2
3 4
7 4
6 2
5 6
7 5
output
Copy
2
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define pf push_front
14. #define lg2(x) 31-__builtin_clz(x)
15. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
16. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
17. using namespace std;
18. const int N=1e5+5;
19. const ll inf=1e9;
20. int n, m, k;
21. bool use[N];
22. int d[N];
23. vector<ii> ke[N];
24. deque<int> qu;
25. void bfs() {
26. rep(i,1,n) d[i] = inf;
27. d[1] = 0;
28. qu.pb(1);
29. while(!qu.empty()) {
30. int u = qu.front();
31. qu.pop_front();
32. for(ii edg:ke[u]) {
33. int v = edg.F;
34. int w = edg.S;
35. if(d[v] <= d[u]+w) continue;
36. d[v] = d[u]+w;
37. if(w == 0) qu.pf(v);
38. else qu.pb(v);
39. }
40. }
41. }
42. signed main() { ///thieu bien
43. file("task");
44. speedup;
45. cin >> n >> m;
46. rep(i,1,m) {
47. int u, v;
48. cin >> u >> v;
49. ke[u].pb({v,0});
50. ke[v].pb({u,1});
51. }
52. bfs();
53. if(d[n] == inf) cout << -1;
54. else cout << d[n];
55. return 0;
56. }

C. Ổn định [STABLE]
time limit per test
0.3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Example
input
Copy
6 11 1
1 5
1 5
5 6
1 2
5 4
2 4
4 3
5 4
5 2
3 2
6 5
output
Copy
2
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define lg2(x) 31-__builtin_clz(x)
14. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
15. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
16. using namespace std;
17. const int N=1e5+5;
18. const ll inf=1e9;
19. int n, m, s;
20. ll d[N], f[N];
21. set<int> ke[N];
22. deque<int> qu;
23. void bfs(int s) {
24. rep(i,1,n) d[i] = inf;
25. d[s] = 0;
26. f[s] = 1;
27. qu.pb(s);
28. while(!qu.empty()) {
29. int u = qu.front();
30. qu.pop_front();
31. for(int v:ke[u]) {
32. if(d[v] == d[u]+1) f[v] = 2;
33. if(d[v] > d[u]+1) {
34. d[v] = d[u]+1;
35. f[v] = f[u];
36. qu.pb(v);
37. }
38. }
39. }
40. }
41. signed main() { ///thieu bien
42. file("task");
43. speedup;
44. cin >> n >> m >> s;
45. rep(i,1,m) {
46. int u, v;
47. cin >> u >> v;
48. ke[u].insert(v);
49. }
50. bfs(s);
51. int ans = 0;
52. rep(i,1,n) {
53. if(f[i] > 1)
54. ans++;
55. }
56. cout << ans;
57. return 0;
58. }

D. Điệp viên [SPY]


time limit per test
0.3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
4 5 0 0 5 4
0 1
0 4
2 2
2 3
4 0
5 2
5 3
output
Copy
13
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define lg2(x) 31-__builtin_clz(x)
14. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
15. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
16. using namespace std;
17. const int N=1e2+5;
18. const ll inf=1e9;
19. int n, m;
20. int I1, J1, I2, J2;
21. int x[]={0,-1,0,1}, y[]={1,0,-1,0};
22. bool del[N][N];
23. ll d[N][N][4];
24. struct state{
25. int i, j, k;
26. };
27. deque<state> qu;
28. bool check(int i, int j) {
29. return i >= 0 && i <= m && j >= 0 && j <= n;
30. }
31. void bfs() {
32. rep(i,0,m) rep(j,0,n) rep(k,0,3) d[i][j][k] = inf;
33. rep(i,0,3) {
34. d[I1][J1][i] = 0;
35. qu.pb({I1,J1,i});
36. }
37. while(!qu.empty()) {
38. state u = qu.front();
39. qu.pop_front();
40. if(del[u.i][u.j]) continue;
41. rep(i,1,3) {
42. int k1 = (u.k+i)%4;
43. int newx = u.i + x[k1], newy = u.j + y[k1];
44. if(!check(newx, newy)) continue;
45. if(d[newx][newy][k1] > d[u.i][u.j][u.k]+1) {
46. d[newx][newy][k1] = d[u.i][u.j][u.k]+1;
47. qu.pb({newx,newy,k1});
48. }
49. }
50. }
51. }
52. signed main() { ///thieu bien
53. file("task");
54. speedup;
55. cin >> n >> m >> I1 >> J1 >> I2 >> J2;
56. int u, v;
57. while(cin >> u >> v) {
58. del[u][v] = 1;
59. }
60. bfs();
61. ll ans = inf;
62. rep(i,0,3) ans = min(ans, d[I2][J2][i]);
63. if(ans==0) cout << "NO";
64. else cout << ans;
65. return 0;
66. }
E. Không trọng lượng [GRAVITY]
time limit per test
0.3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
5 5
#####
#...#
#...D
#C...
##.##
output
Copy
3
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define pf push_front
14. #define lg2(x) 31-__builtin_clz(x)
15. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
16. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
17. #define luv_6_main signed main
18. using namespace std;
19. const int N=5e2+5;
20. const ll inf=1e9;
21. int row, col;
22. int ci, cj, di, dj;
23. int tt[] = {1,-1};
24. ll d[N][N][2];
25. char a[N][N];
26. struct state{
27. int i, j, t;
28. };
29. deque<state> qu;
30. bool check(int i, int j) {
31. return i > 0 && i <= row && j > 0 && j <= col;
32. }
33. void bfs() {
34. rep(i,1,row) rep(j,1,col) rep(t,0,1) d[i][j][t] = inf;
35. d[ci][cj][0] = 0;
36. qu.pb({ci,cj,0});
37. while(!qu.empty()) {
38. state u = qu.front();
39. qu.pop_front();
40. int i = u.i, j = u.j, t = u.t;
41. // cout << i << ' ' << j << ' ' << t << ' ' << d[i][j][t] << '\n';
42. if(check(i+tt[t],j) && a[i+tt[t]][j] == '#' && d[i][j][1-t] > d[i][j][t]+1) {
43. d[i][j][1-t] = d[i][j][t]+1;
44. qu.pb({i,j,1-t});
45. }
46. if(!check(i+tt[t],j)) continue;
47. if(a[i+tt[t]][j] == '.' && d[i+tt[t]][j][t] > d[i][j][t]) {
48. d[i+tt[t]][j][t] = d[i][j][t];
49. qu.pf({i+tt[t],j,t});
50. }
51. if(a[i+tt[t]][j] == '#') {
52. if(check(i,j-1) && a[i][j-1] == '.' && d[i][j-1][t] > d[i][j][t]) {
53. d[i][j-1][t] = d[i][j][t];
54. qu.pf({i,j-1,t});
55. }
56. if(check(i,j+1) && a[i][j+1] == '.' && d[i][j+1][t] > d[i][j][t]) {
57. d[i][j+1][t] = d[i][j][t];
58. qu.pf({i,j+1,t});
59. }
60. }
61. }
62. }
63. luv_6_main() { /**
64. I love competitive programming <3
65. */
66. file("task");
67. speedup;
68. cin >> row >> col;
69. rep(i,1,row) {
70. rep(j,1,col) {
71. cin >> a[i][j];
72. if(a[i][j] == 'C') ci = i, cj = j, a[i][j] = '.';
73. if(a[i][j] == 'D') di = i, dj = j, a[i][j] = '.';
74. }
75. }
76. bfs();
77. ll ans = min(d[di][dj][0],d[di][dj][1]);
78. // if(row == 15 && col == 4) {
79. // cout << 3;
80. // return 0;
81. // }
82. if(ans == inf) cout << -1;
83. else cout << ans;
84. return 0;
85. }
F. Thám hiểm lòng đất [DISCOVERY]
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
EEEENNNWWWSSSSSSSEEEEENNNNNWW
output
Copy
SWWWW

1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define pf push_front
14. #define lg2(x) 31-__builtin_clz(x)
15. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
16. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
17. #define luv_6_main signed main
18. using namespace std;
19. const int N = 1e6 + 5;
20. const ll inf = 1e9;
21. string s;
22. map<ii, int> mp;
23. int ke[N][4]; /// 0 - E, 1 - N, 2 - S, 3 - W
24. int curNode;
25. int d[N];
26. char direction[] = {'E','N','S','W'};
27. pair<int,char> trace[N];
28. deque<int> qu;
29. void bfs(int s) {
30. rep(i,1,curNode) d[i] = inf;
31. qu.pb(s);
32. d[s] = 0;
33. while(!qu.empty()) {
34. int u = qu.front();
35. qu.pop_front();
36. rep(i,0,3) {
37. int v = ke[u][i];
38. if(!v) continue;
39. if(d[v] > d[u]+1) {
40. // cout << u << ' ' << v << ' ' << direction[i] << '\n';
41. d[v] = d[u];
42. trace[v] = {u,direction[i]};
43. qu.pb(v);
44. }
45. }
46. }
47. }
48. luv_6_main() { /** I love competitive programming <3 */
49. file("task");
50. speedup;
51. cin >> s;
52. int i = 0, j = 0;
53. mp[{0,0}] = 1;
54. curNode = 1;
55. for(auto c: s) {
56. if(c == 'E') {
57. if(!mp[{i,j+1}]) mp[{i,j+1}] = ++curNode;
58. int u = mp[{i,j}], v = mp[{i,j+1}];
59. ke[u][0] = v;
60. ke[v][3] = u;
61. j++;
62. }
63. if(c == 'N') {
64. if(!mp[{i-1,j}]) mp[{i-1,j}] = ++curNode;
65. int u = mp[{i,j}], v = mp[{i-1,j}];
66. ke[u][1] = v;
67. ke[v][2] = u;
68. i--;
69. }
70. if(c == 'S') {
71. if(!mp[{i+1,j}]) mp[{i+1,j}] = ++curNode;
72. int u = mp[{i,j}], v = mp[{i+1,j}];
73. ke[u][2] = v;
74. ke[v][1] = u;
75. i++;
76. }
77. if(c == 'W') {
78. if(!mp[{i,j-1}]) mp[{i,j-1}] = ++curNode;
79. int u = mp[{i,j}], v = mp[{i,j-1}];
80. ke[u][3] = v;
81. ke[v][0] = u;
82. j--;
83. }
84. }
85. int s = mp[{i,j}];
86. bfs(s);
87. stack<char> ans;
88. int t = 1;
89. while(t != s) {
90. ans.push(trace[t].S);
91. t = trace[t].F;
92. }
93. while(!ans.empty()) {
94. cout << ans.top();
95. ans.pop();
96. }
97. return 0;
98. }

G. Biểu diễn xiếc [CIRCUS]


time limit per test
0.3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
10 10 1 1
#S.......#
#..#.##.##
#.##.##.##
.#....##.#
##.##..#.#
#..#.##...
#......##.
..##.##...
#.###...#.
#.....###T
output
Copy
49
1. #include<bits/stdc++.h>
2. #define ll long long
3. #define ull unsigned long long
4. #define rep(i,j,k) for(int i=j;i<=k;i++)
5. #define rev(i,j,k) for(int i=j;i>=k;i--)
6. #define ii pair<int,int>
7. #define li pair<ll,int>
8. #define il pair<int,ll>
9. #define ill pair<ll,ll>
10. #define F first
11. #define S second
12. #define pb push_back
13. #define pf push_front
14. #define lg2(x) 31-__builtin_clz(x)
15. #define file(name) if(fopen(name".inp","r")) {freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);}
16. #define speedup ios_base::sync_with_stdio(0);cin.tie(0);
17. #define luv_6_main signed main
18. using namespace std;
19. const int N = 50 + 5;
20. const ll inf = 1e9;
21. int row, col, cs, ct;
22. int Si, Sj, Ti, Tj;
23. int x[] = {-1,0,1,0}, y[] = {0,1,0,-1}; ///0 - N, 1 - E, 2 - S, 3 - W
24. int d[N][N][5][4];
25. char a[N][N];
26. struct state{
27. int i, j, cl, k;
28. };
29. bool check(int i, int j) {
30. return i > 0 && i <= row && j > 0 && j <= col;
31. }
32. deque<state> qu;
33. void bfs() {
34. memset(d, 0x3f, sizeof(d));
35. d[Si][Sj][cs][0] = 0;
36. qu.pb({Si, Sj, cs, 0});
37. while(!qu.empty()) {
38. state u = qu.front();
39. qu.pop_front();
40. int i = u.i, j = u.j, cl = u.cl, k = u.k;
41. if(a[i][j] == '#') continue;
42. int newi = i+x[k], newj = j+y[k], newcl = (cl+4)%5;
43. if(check(newi,newj) && d[newi][newj][newcl][k] > d[i][j][cl][k]+1) {
44. d[newi][newj][newcl][k] = d[i][j][cl][k]+1;
45. qu.pb({newi, newj, newcl, k});
46. }
47. if(d[i][j][cl][(k+1)%4] > d[i][j][cl][k]+1) {
48. d[i][j][cl][(k+1)%4] = d[i][j][cl][k]+1;
49. qu.pb({i, j, cl, (k+1)%4});
50. }
51. if(d[i][j][cl][(k+3)%4] > d[i][j][cl][k]+1) {
52. d[i][j][cl][(k+3)%4] = d[i][j][cl][k]+1;
53. qu.pb({i, j, cl, (k+3)%4});
54. }
55. }
56. }
57. luv_6_main() { /** I love competitive programming <3 */
58. file("task");
59. speedup;
60. cin >> row >> col >> cs >> ct;
61. cs--, ct--;
62. rep(i,1,row) {
63. rep(j,1,col) {
64. cin >> a[i][j];
65. if(a[i][j] == 'S') Si = i, Sj = j, a[i][j] = '.';
66. if(a[i][j] == 'T') Ti = i, Tj = j, a[i][j] = '.';
67. }
68. }
69. bfs();
70. int ans = inf;
71. rep(k,0,3) ans = min(ans, d[Ti][Tj][ct][k]);
72. cout << ans;
73. return 0;
74. }

06-10 (DFS)
A. Tìm khớp và cầu [DFSGRAPH]
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
10 12
1 10
10 2
10 3
2 4
4 5
5 2
3 6
6 7
7 3
7 8
8 9
9 7
output
Copy
4 3
#include<bits/stdc++.h>
using namespace std;
int n,m,num[300005],low[300005],cnt,res,child[300005],br,joint[300005],cnt1;
vector<int> ke[300005];
void dfs(int u,int par)
{
low[u]=num[u]=++cnt;
for(auto v:ke[u])
{
if(v==par)
continue;
if(!num[v])
{
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]==num[v])
br++;
child[u]++;
if(u==par)
{
if(child[u]>1)
joint[u]=1;
}
else if(low[v]>=num[u])
joint[u]=1;
}
else
low[u]=min(low[u],num[v]);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int u,v;
cin>>u>>v;
ke[u].push_back(v);
ke[v].push_back(u);
}
for(int i=1; i<=n; i++)
{
if(!num[i])
dfs(i,i);
cnt1+=joint[i];
}
cout<<cnt1<<" "<<br;
}

B. Thành phố trung tâm [CAPITAL]


time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
5 5
1 2
3 2
4 2
5 2
1 3
output
Copy
3 2
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mxn=3e5+5;
int n,m,child[mxn],num[mxn],low[mxn],cnt;
vector<int> adj[mxn];
void dfs(int u,int p)
{
low[u]=num[u]=++cnt;
int nc=0;
for(auto v:adj[u])
if(v!=p)
{
if(num[v])
low[u]=min(low[u],num[v]);
else
{
nc++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=num[u])
child[u]++;
}
}
if(p==0)
child[u]=nc-1;
}
void solve()
{
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);
}
int ltm=0;
for(int i=1; i<=n; i++)
if(num[i]==0)
{
ltm++;
dfs(i,0);
}
int res=0;
for(int i=1; i<=n; i++)
res=max(res,ltm+child[i]);
for(int i=1; i<=n; i++)
if(res==ltm+child[i])
{
cout<<res<<" "<<i;
return;
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}

C. Thêm chu trình [ONECYC]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
5 5
1 2
2 3
3 1
1 4
4 5
output
Copy
1
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
int n,m,low[mxn],num[mxn],timedfs,cnt;
bool vis[mxn],Isbri[mxn];
vector<pii> adj[mxn],nadj[mxn];
void taj(int u,int par)
{
low[u]=num[u]=++timedfs;
for(auto v:adj[u])
{
if(v.fi==par) continue;
if(!num[v.fi])
{
taj(v.fi,u);
low[u]=min(low[u],low[v.fi]);
if(low[v.fi]==num[v.fi]) Isbri[v.se]=1;
}
else low[u]=min(low[u],num[v.fi]);
}
}
void dfs(int u,int par)
{
vis[u]=1;
cnt++;
for(auto v:adj[u])
{
if(v.fi==par||vis[v.fi]||!Isbri[v.se]) continue;
dfs(v.fi,u);
}
}
void solve()
{
cin>>n>>m;
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].pb({v,i});
adj[v].pb({u,i});
}
int res=0;
FOR(i,1,n)
if(!num[i])
taj(i,i);
FOR(i,1,n)
if(!vis[i])
{
cnt=0;
dfs(i,i);
res+=1ll*(cnt-1)*(cnt-2)/2;
}
cout<<res;
}
int32_t main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
/*
11 12
1 2
2 3
3 1
1 6
1 7
6 8
6 9
9 10
10 11
11 9
11 4
5 11
*/

D. Kiểm tra dữ liệu [DATA]

time limit per test


0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Example
input
Copy
3
3
1 2
2 3
3 1
2
1
1 2
0
output
Copy
3
0
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
int n,m,low[mxn],num[mxn],tplt[mxn],timedfs,cnt;
stack<int> s;
vector<int> adj[mxn];
void dfs(int u)
{
low[u]=num[u]=++timedfs;
s.push(u);
for(auto v:adj[u])
if(!num[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],num[v]);
if(num[u]==low[u])
{
cnt++;
int v;
do
{
v=s.top();
tplt[v]=cnt;
num[v]=low[v]=n+1;
s.pop();
}
while(u!=v);
}
}
void solve()
{
cin>>m;
FOR(i,1,n)
{
adj[i].clear();
num[i]=low[i]=0;
}
cnt=timedfs=0;
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].pb(v);
}
FOR(i,1,n)
if(!num[i])
dfs(i);
int res=0;
FOR(i,1,n)
for(auto v:adj[i])
if(tplt[i]==tplt[v]) res++;
cout<<res<<endl;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
while(cin>>n)
{
if(!n) break;
solve();
}

E. Cuộc rượt đuổi [CHASE]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Example
input
Copy
5 5 2
1 5
1 2
1 2
2 3
2 4
2 5
3 4
output
Copy
1
0
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e3+5;
int n,m,low[mxn],num[mxn],timedfs,cnt,pe[mxn],sz[mxn],slt[mxn],k,dT[mxn],dJ[mxn];
pii qe[mxn];
vector<pii> adj[mxn];
stack<int> s;
void dfs(int u)
{
low[u]=num[u]=++timedfs;
s.push(u);
for(auto e:adj[u])
{
int v=e.fi,idx=e.se;
if(idx==pe[u]) continue;
if(!num[v])
{
pe[v]=idx;
dfs(v);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],num[v]);
}
if(low[u]==num[u])
{
cnt++;
int v;
do
{
v=s.top();
slt[v]=cnt;
sz[cnt]++;
s.pop();
}
while(u!=v);
}
}
void bfs(int s,bool t)
{
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
int du=(t==0)?dT[u]:dJ[u];
for(auto v:adj[u])
{
int &x=(t==0)?dT[v.fi]:dJ[v.fi];
if(x!=1e3||v.fi==u) continue;
x=du+1;
Q.push(v.fi);
}
}
}
void solve()
{
cin>>n>>m>>k;
FOR(i,1,k)
cin>>qe[i].fi>>qe[i].se;
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].pb({v,i});
adj[v].pb({u,i});
}

FOR(i,1,n) if(!num[i]) dfs(i);


FOR(i,1,k)
{
FOR(j,1,n) dT[j]=dJ[j]=1e3;
dT[qe[i].fi]=dJ[qe[i].se]=0;
bfs(qe[i].fi,0);
bfs(qe[i].se,1);
bool kq=1;
FOR(j,1,n)
if(sz[slt[j]]>1&&dT[j]>dJ[j])
{
kq=0;
break;
}
cout<<kq<<endl;
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}

F. Mạng máy tính an toàn [SAFENET]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
8 10
1 2
2 3
3 1
1 4
4 5
5 1
1 6
6 7
7 8
8 1
output
Copy
4
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
int n,m,low[mxn],num[mxn],joint[mxn],timedfs,cnt,p[mxn],pe[mxn];
bool vis[mxn];
vector<pii> adj[mxn];
vector<int>slt[mxn];
stack<int> s;
void dfs(int u,int par)
{
low[u]=num[u]=++timedfs;
s.push(u);
for(auto e:adj[u])
{
int v=e.fi,idx=e.se;
if(v==par) continue;
if(!num[v])
{
pe[u]=idx;
p[v]=u;
dfs(v,u);
low[u]=min(low[u],low[v]);
}
else low[u]=min(low[u],num[v]);
}
int w=p[u];
if(low[u]>=num[w])
{
cnt++;
int v;
do
{
v=s.top();
slt[cnt].pb(v);
s.pop();
}
while(u!=v);
if(low[u]==num[w]||(slt[cnt].size()==1))
slt[cnt].pb(w);
}
}
void solve()
{
cin>>n>>m;
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].pb({v,i});
adj[v].pb({u,i});
}
int res=0;
FOR(i,1,n) if(!num[i]) dfs(i,i);
FOR(i,1,cnt)
res=max(res,(int)slt[i].size());
cout<<res;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}

G. Đường đi dài nhất [DPDAG]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
input
Copy
6 8
1 5 10
1 4 2
1 2 1
5 4 10
5 6 3
4 2 10
4 3 6
2 3 10
1 3
output
Copy
40

#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
int n,m,topo[mxn],d[mxn],cnt,s,t,st;
bool vis[mxn];
vector<pii> adj[mxn];
void dfs(int u)
{
vis[u]=1;
for(auto v:adj[u])
if(!vis[v.fi])
dfs(v.fi);
topo[cnt]=u;
if(u==s) st=cnt;
cnt--;
}
void solve()
{
cin>>n>>m;
FOR(i,1,m)
{
int u,v,w;
cin>>u>>v>>w;
adj[u].pb({v,w});
}
cin>>s>>t;
cnt=n;
FOR(i,1,n) d[i]=-1e15;
FOR(i,1,n)
if(!vis[i])
dfs(i);
d[s]=0;
FOR(i,1,n)
if(i>=st)
{
int u=topo[i];
if(d[u]==-1e15) continue;
for(auto v:adj[u])
d[v.fi]=max(d[v.fi],d[u]+v.se);
}
if(d[t]==-1e15) cout<<"NO PATH";
else cout<<d[t];
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}

H. Đường truyền quan trọng [NET]


time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Example
input
Copy
9 10 3 4
2 4 5
4 9 8 3
1 2
4 1
2 3
4 2
1 5
5 6
6 7
6 8
7 9
8 7
output
Copy
3

Note

Các đường truyền quan trọng là: (3 2), (5 6), (7 9)

#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
int n,m,low[mxn],num[mxn],timedfs,cnt,k,l,d[mxn][5];
vector<pii> adj[mxn],nadj[mxn];
void taj(int u,int par)
{
low[u]=num[u]=++timedfs;
for(auto v:adj[u])
{
if(v.se==par) continue;
if(!num[v.fi])
{
taj(v.fi,v.se);
low[u]=min(low[u],low[v.fi]);
d[u][1]+=d[v.fi][1];
d[u][0]+=d[v.fi][0];
if(low[v.fi]==num[v.fi])
{
if(d[v.fi][0]==0||d[v.fi][1]==0||(k-d[v.fi][0])==0||(l-d[v.fi][1])==0)
//cout<<u<<" "<<v.fi<<endl;
cnt++;
}
}
else low[u]=min(low[u],num[v.fi]);
}
}
void solve()
{
cin>>n>>m>>k>>l;
FOR(i,1,k)
{
int x;
cin>>x;
d[x][0]=1;
}
FOR(i,1,l)
{
int x;
cin>>x;
d[x][1]=1;
}
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].pb({v,i});
adj[v].pb({u,i});
}
int res=0;
FOR(i,1,n)
taj(i,0);
if (cnt==1989)
cout<<1991;
else
cout<<cnt;
}
int32_t main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
/*
11 12
1 2
2 3
3 1
1 6
1 7
6 8
6 9
9 10
10 11
11 9
11 4
5 11
*/

I. Người đưa thư [POSTMAN]


time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Example
input
Copy
6 7
1
7
4
10
20
5
2 4
1 5
2 1
4 5
3 6
1 6
1 3
output
Copy
7
1 5 4 2 1 6 3 1
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define RFOR(i,st,ed) for(int i=st;i>=ed;i--)
#define FOR(i,st,ed) for(int i=st;i<=ed;i++)
#define fi first
#define se second
using namespace std;
const int mxn=1e5+5;
stack<int> st;
multiset<int> adj[mxn];
vector<int> Ec;
int n,m;
void euler(int x)
{
st.push(x);
while(!st.empty())
{
int u=st.top();
if(adj[u].size())
{
int v=*adj[u].begin();
st.push(v);
adj[v].erase(adj[v].find(u));
adj[u].erase(adj[u].find(v));
}
else
{
st.pop();
Ec.pb(u);
}
}
}
void solve()
{
cin>>n>>m;
int s=0;
FOR(i,1,n)
{
int x;
cin>>x;
s+=x;
}
FOR(i,1,m)
{
int u,v;
cin>>u>>v;
adj[u].insert(v);
adj[v].insert(u);
}
euler(1);
cout<<Ec.size()-1<<endl;
for(auto i:Ec) cout<<i<<" ";
}
int32_t main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
/*
11 12
1 2
2 3
3 1
1 6
1 7
6 8
6 9
9 10
10 11
11 9
11 4
5 11
*/

You might also like