牛客网:核弹剑仙(DFS)
传送门
思路:
dfs寻找比目前查找的武器的杀伤力大的武器,知道找不到为止
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define T int t ;cin >> t;while(t--)
#define Casein int Case ; for (scanf("%d", &Case); Case; Case --)
#define cio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//#pragma GCC2 optimize(2)//¿ªÆôO2¼ÓËÙÆ÷(ĬÈÏ)
//#pragma GCC3 optimize(3,"Ofast","inline")
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d%d", &a, &b)
#define sc3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scl(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld%lld", &a, &b)
#define ss(a) scanf("%s", a)
#define mem(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
#define cl(a) printf("%lld",a)
#define c(a) printf("%d",a)
#define CY printf("Yes\n")
#define CN printf("No\n")
#define ll_INF 0x3f3f3f3f3f3f3f3f
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-11;
const ll mod = 1e9 + 7;
int n , m ;
vector<int> v[maxn] ;
int ans ;
int vis[maxn] ;
void dfs(int x){
for(int i = 0 ; i < v[x].size(); i++){
if(!vis[v[x][i]]){
vis[v[x][i]] = 1 ;
dfs(v[x][i]) ;
}
}
}
int main(){
cin >> n >> m ;
while(m--){
int a,b ;
cin >> a >> b;
v[b].push_back(a) ;
}
for(int i = 1 ; i <= n ; i++){
ans = 0 ;
dfs(i) ;
for(int j = 0 ; j <= n ; j++) ans += vis[j],vis[j] = 0 ;
cout << ans << endl ;
}
}