Code (C++)
#include<bits/stdc++.h>
using namespace std;
int n, m, k;
char a[1001][1001];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void dfs(int i, int j, int &cnt){
a[i][j] = '.';
++cnt;
for(int id=0; id<4; id++){
int ni = i + dx[id];
int nj = j + dy[id];
if(1 <= ni and ni <= n and 1 <= nj and nj <= m and a[ni][nj] == '#'){
dfs(ni, nj, cnt);
}
}
}
void solve(){
cin >> n >> m >> k;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
a[i][j] = '.';
}
}
for(int i=1; i<=k; i++){
int x, y; cin >> x >> y;
a[x][y] = '#';
}
int res = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(a[i][j] == '#'){
int cnt = 0;
dfs(i, j, cnt);
res = max(res, cnt);
}
}
}
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
int n, ok[101][101], vis[101];
vector<int> a[101];
void dfs(int &u){
vis[u] = 1;
for(int &v : a[u]){
if(!vis[v] and ok[u][v]) dfs(v);
}
}
int check(int i, int j){
for(int u=1; u<=n; u++) vis[u] = 0;
ok[i][j] = 0;
int res = 0;
for(int u=1; u<=n; u++){
if(!vis[u]){
dfs(u);
++res;
}
}
ok[i][j] = 1;
return res;
}
void solve(){
cin >> n;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >> ok[i][j];
if(ok[i][j]) a[i].push_back(j);
}
}
int tplt = 0;
for(int i=1; i<=n; i++){
if(!vis[i]){
dfs(i);
++tplt;
}
}
int res = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(ok[i][j] and check(i, j) > tplt){
++res;
}
}
}
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, m, d[mxn], sz[mxn];
int get(int u){
if(u == d[u]) return u;
return d[u] = get(d[u]);
}
void uni(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(x > y) swap(x, y);
d[y] = x;
sz[x] += sz[y];
}
void solve(){
cin >> n >> m;
for(int i=1; i<=n; i++){
d[i] = i;
sz[i] = 1;
}
while(m--){
int x, y; cin >> x >> y;
uni(x, y);
}
int mx = 0;
for(int i=2; i<=n; i++){
int x = get(i);
if(x > 1){
mx = max(mx, sz[x]);
}
}
cout << sz[1] + mx;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
583184D - Điểm nghẽn giao thông
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn
int n, m, s, t;
vector<int> a[105];
bool vis[105];
void dfs(int u){
vis[u] = 1;
for(int &v : a[u]){
if(!vis[v]) dfs(v);
}
}
void solve(){
cin >> n >> m >> s >> t;
while(m--){
int x, y; cin >> x >> y;
a[x].push_back(y);
}
int res = 0;
for(int i=1; i<=n; i++){
memset(vis, 0, sizeof vis);
vis[i] = 1;
dfs(s);
if(!vis[t]) ++res;
}
cout << res << el;
for(int i=1; i<=n; i++) a[i].clear();
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; cin >> tc;
while(tc--) solve();
}
583184E - Số lần duyệt ít nhất
Code (C++)
#include<bits/stdc++.h>
using namespace std;
const int mxn = 1e3 + 7;
int n, m;
vector<int> a[mxn];
bool del[mxn], vis[mxn];
void dfs(int u){
vis[u] = 1;
for(int &v : a[u]){
if(!vis[v]) dfs(v);
}
}
void solve(){
cin >> n >> m;
while(m--){
int x, y; cin >> x >> y;
a[x].push_back(y);
}
for(int i=1; i<=n; i++){
for(int &j : a[i]){
del[j] = 1;
}
}
int res = 0;
for(int i=1; i<=n; i++){
if(!del[i]){
dfs(i);
++res;
}
}
for(int i=1; i<=n; i++){
if(!vis[i]){
dfs(i);
++res;
}
}
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, m, a[mxn], p[mxn], c[mxn];
int get(int u){
if(u == p[u]) return u;
return p[u] = get(p[u]);
}
void solve(){
cin >> n >> m;
for(int i=1; i<=n; i++){
cin >> a[i];
p[i] = i;
c[i] = a[i];
}
while(m--){
int x, y; cin >> x >> y;
x = get(x); y = get(y);
if(c[x] < c[y]) p[y] = x;
else p[x] = y;
}
long long res = 0;
for(int i=1; i<=n; i++){
int u = get(i);
res += c[u];
c[u] = 0;
}
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 7;
int n, k, m; // các biến đề cho
int f[mxn]; // f[i]: thời gian tối thiểu người thứ i đến được lối thoát
vector<int> a[mxn]; // danh sách kề
bool ex[mxn]; // ex[i] = 1 -> lối thoát, ex[i] = 0 -> phòng bình thường
void bfs(int s){
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
for(int &v : a[u]){
// phòng v không phải lối thoát và
// cách đi này tốn ít thời gian hơn các cách trước
if(!ex[v] and f[u] + 1 < f[v]){
f[v] = f[u] + 1;
q.push(v);
}
}
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
// ban đầu gán giá trị lớn cho thời gian
for(int i=1; i<=n; i++) f[i] = 1e8;
while(k--){
int room; cin >> room;
ex[room] = 1; // room là lối thoát
f[room] = 0; // không tốn thời gian di chuyển
}
cin >> m;
while(m--){
// thiết kế danh sách kề
int x, y; cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
// BFS đa luồng -> xuất phát từ các lối thoát
// đi đến các phòng khác để tìm thời gian tối thiểu
for(int i=1; i<=n; i++) if(ex[i]) bfs(i);
// in ra kết quả
for(int i=1; i<=n; i++) cout << f[i] << ' ';
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 10004
int p[mxn];
int get(int u){
if(u == p[u]) return u;
return p[u] = get(p[u]);
}
void solve(){
int n, q; cin >> n >> q;
for(int i=1; i<=n; i++) p[i] = i;
while(q--){
int x, y, z; cin >> x >> y >> z;
if(z == 1){
x = get(x); y = get(y);
if(x > y) swap(x, y);
p[y] = x;
}else{
cout << (get(x) == get(y) ? "Yes\n" : "No\n");
}
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, q, a[mxn], p[mxn];
map<int, int> f[mxn];
int get(int u){
if(u == p[u]) return u;
return p[u] = get(p[u]);
}
void solve(){
cin >> n >> q;
for(int i=1; i<=n; i++){
cin >> a[i];
p[i] = i;
++f[i][a[i]];
}
while(q--){
int t, x, y; cin >> t >> x >> y;
if(t == 1){
x = get(x); y = get(y);
if(x == y) continue;
if(x > y) swap(x, y);
p[y] = x;
for(auto &[i, j] : f[y]){
f[x][i] += j;
}
}else{
x = get(x);
cout << f[x][y] << el;
}
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
583184J - Đường bộ và đường sắt
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define pe pair<int, int>
int n, m, k, p[2][200005];
map<pe, int> mp;
int get(int u, int p[]){
if(u == p[u]) return u;
return p[u] = get(p[u], p);
}
void solve(){
cin >> n >> m >> k;
for(int i=1; i<=n; i++){
p[0][i] = p[1][i] = i;
}
for(int i=0; i<m; i++){
int x, y; cin >> x >> y;
x = get(x, p[0]);
y = get(y, p[0]);
if(x > y) swap(x, y);
p[0][y] = x;
}
for(int i=0; i<k; i++){
int x, y; cin >> x >> y;
x = get(x, p[1]);
y = get(y, p[1]);
if(x > y) swap(x, y);
p[1][y] = x;
}
for(int i=1; i<=n; i++){
++mp[{get(i, p[0]), get(i, p[1])}];
}
for(int i=1; i<=n; i++){
cout << mp[{p[0][i], p[1][i]}] << ' ';
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define pe pair<ld, ld>
#define fi first
#define se second
#define el '\n'
#define ld long double
struct canh{
int u, v;
ld w;
};
int n, m, p[1005];
vector<canh> v;
int get(int u){
if(u == p[u]) return u;
return p[u] = get(p[u]);
}
ld dis(pe a, pe b){
return sqrtl((a.fi - b.fi) * (a.fi - b.fi) + (a.se - b.se) * (a.se - b.se));
}
bool cmp(canh a, canh b){
return a.w < b.w;
}
void solve(){
cin >> n >> m;
pe a[n+5];
for(int i=1; i<=n; i++){
p[i] = i;
}
for(int i=1; i<=n; i++){
cin >> a[i].fi >> a[i].se;
}
for(int i=1; i<=m; i++){
int x, y; cin >> x >> y;
x = get(x); y = get(y);
if(x == y) continue;
p[y] = x;
}
for(int i=1; i<n; i++){
for(int j=i+1; j<=n; j++){
v.push_back({i, j, dis(a[i], a[j])});
}
}
sort(v.begin(), v.end(), cmp);
ld mst = 0;
int sz = 0;
for(int i=0; i<v.size() and sz < n - 1; i++){
canh c = v[i];
int x = get(c.u), y = get(c.v);
if(x == y) continue;
p[y] = x;
mst += c.w;
++sz;
}
cout << fixed << setprecision(2) << mst;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
583184L - Truyền tin trong mạng
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define pe pair<int, int>
#define fi first
#define se second
#define el '\n'
#define mxn 100005
#define int long long
int n, m, s, t, dp[mxn];
vector<pe> a[mxn];
void dijkstra(){
for(int i=1; i<=n; i++) dp[i] = 1e16;
dp[s] = 0;
priority_queue<pe, vector<pe>, greater<pe>> q;
q.push({dp[s], s});
while(!q.empty()){
auto top = q.top(); q.pop();
int u = top.se;
int kc = top.fi;
if(kc > dp[u]) continue;
for(auto &i : a[u]){
int v = i.fi;
int w = i.se;
if(dp[u] + w < dp[v]){
dp[v] = dp[u] + w;
q.push({dp[v], v});
}
}
}
cout << dp[t];
}
void solve(){
cin >> n >> m >> s >> t;
while(m--){
int x, y, w; cin >> x >> y >> w;
a[x].push_back({y, w});
a[y].push_back({x, w});
}
dijkstra();
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, sz[mxn];
vector<int> a[mxn];
void dfs(int u, int p = 0){
sz[u] = 1;
for(int &v : a[u]){
if(v != p){
dfs(v, u);
sz[u] += sz[v];
}
}
}
void solve(){
cin >> n;
for(int i=1; i<n; i++){
int x, y; cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
for(int i=1; i<=n; i++) cout << sz[i] << ' ';
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, d[mxn];
vector<int> a[mxn];
void dfs(int u, int p = 0){
for(int &v : a[u]){
if(v != p){
d[v] = d[u] + 1;
dfs(v, u);
}
}
}
void solve(){
int k;
cin >> n >> k;
for(int i=1; i<n; i++){
int x, y; cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
sort(d+1, d+n+1, greater<int>());
long long res = 0;
for(int i=1; i<=k; i++) res += d[i];
cout << res;
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 100005
int n, res, dp[mxn];
vector<int> a[mxn];
string s;
void dfs(int u, int p = 0){
if(s[u] == 'W') ++dp[u];
else --dp[u];
for(int &v : a[u]){
if(v != p){
dfs(v, u);
dp[u] += dp[v];
}
}
if(dp[u] == 0) ++res;
}
void solve(){
cin >> n;
for(int i=2; i<=n; i++){
int p; cin >> p;
a[p].push_back(i);
a[i].push_back(p);
}
cin >> s; s = " " + s;
res = 0;
dfs(1);
cout << res << el;
for(int i=1; i<=n; i++){
dp[i] = 0;
a[i].clear();
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; cin >> tc;
while(tc--) solve();
}
Code (C++)
#include<bits/stdc++.h>
using namespace std;
#define el '\n'
#define mxn 303
int n, m, q, d[mxn][mxn];
void solve(){
cin >> n >> m >> q;
for(int i=1; i<n; i++){
for(int j=i+1; j<=n; j++){
d[i][j] = d[j][i] = 1e9;
}
}
while(m--){
int x, y, w; cin >> x >> y >> w;
d[x][y] = w;
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));
}
}
}
while(q--){
int x, y; cin >> x >> y;
if(d[x][y] != 1e9) cout << d[x][y] << el;
else cout << "-1\n";
}
}
signed main(){
cin.tie(0) -> sync_with_stdio(0);
int tc = 1; //cin >> tc;
while(tc--) solve();
}