DFS
#include <bits/stdc++.h>
using namespace std;
int n;
const int SIZE = 1e4+5;
char area[SIZE][SIZE];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int ans = 0;
int res_ans = 0;
bool flag;
int cnt = 0;
#define CHECK(a, b)(a < n && a >= 0 && b >= 0 && b < n)
void dfs(int x,int y){
if(!flag) cnt = 0;
for(int i = 0;i < 4;i ++){
int dx = x + d[i][0];
int dy = y + d[i][1];
if(area[dx][dy] != '.') cnt ++;
}
if(cnt == 4){
ans ++;
flag = true;
}
area[x][y] = '*';
for(int i = 0;i < 4;i ++){
int xx = x + d[i][0];
int yy = y + d[i][1];
if(area[xx][yy] == '#' && CHECK(xx, yy)) dfs(xx, yy);
}
}
int main(){
cin >> n;
for(int i = 0;i < n;i ++)
for(int j = 0;j < n;j ++)
cin >> area[i][j];
for(int i = 0;i < n;i ++)
for(int j = 0;j < n;j ++)
if(area[i][j] == '#'){
flag = false;
res_ans ++;
dfs(i, j);
}
cout << res_ans -ans;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n, m, k, t;
int tim[2001];
bool have[2001];
struct node{
int a;
int b;
};
struct node fa[100005];
int dfs(int num){
if(have[num]) return 0;
int a = fa[num].a;
int b = fa[num].b;
int d1 = dfs(a);
int d2 = dfs(b);
have[num] = true;
return max(d1, d2) + max(tim[a], tim[b]);
}
int main(){
cin >> n >> m >> k >> t;
for(int i = 1;i <= n;i ++) cin >> tim[i];
for(int i = 0;i < m;i ++){
int temp;
cin >> temp;
have[temp] = true;
}
for(int i = 0;i < k;i ++){
int a, b, temp;
cin >> a >> b >> temp;
fa[temp].a = a;
fa[temp].b = b;
}
cout << dfs(t);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 10e5 + 10;
int n;
int a[SIZE];
int vis[SIZE];
int temp = 0;
int it;
int dfs(int now){
if(a[now] == it){
temp ++;
return 0;
}
if(a[now] != it && !vis[now]){
temp ++;
vis[now] = 1;
dfs(a[now]);
vis[now] = 0;
}
if(a[now] != it && vis[now]) return 0;
return temp;
}
int main(){
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
int max1 = -1;
for(int i = 1;i <= n;i ++){
it = i;
max1 = max(max1, dfs(i));
temp = 0;
}
cout << max1;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
struct node{
vector<int> next;
int ingree;
int degree;
};
node arr[100005];
void dfs(){
queue<int> mq;
for(int i = 1;i <= n;i ++)
if(arr[i].ingree == 1)
mq.push(i);
while(!mq.empty()){
int top = mq.front();
mq.pop();
for(int i = 0;i < arr[top].next.size();i ++){
int nextindex = arr[top].next[i];
arr[nextindex].ingree --;
if(arr[nextindex].ingree == 1)
mq.push(nextindex);
}
}
for(int i = 1;i <= n;i ++)
if(arr[i].ingree > 1)
cout << i << " ";
}
int main(){
cin >> n;
for(int i = 1;i <= n;i ++){
int from, to;
cin >> from >> to;
arr[from].ingree ++;
arr[from].ingree ++;
arr[from].next.push_back(to);
arr[to].next.push_back(from);
}
dfs();
return 0;
}