BFS满江红

P5908 猫猫和企鹅

@[yhdxg](/user/1050431) 看看我的。 ```cpp #include <iostream> #include <vector> #include <queue> using namespace std; #define hh printf("\n") #define kg printf(" ") #define cg ch=getchar() inline int read(){ int x=0,f=1;char cg; while (ch<'0'||ch>'9'){if(ch=='-')f=-1;cg;} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';cg;} return x*f; } inline void write(int x){ if(x<0){putchar('-');x=-x;} if(x>9)write(x/10); putchar(x%10+'0'); } struct csp{ int x,t; }; int n,d,ans; bool vis[100010]; vector<int>a[100010]; int bfs(int x,int t){ queue<csp>q; q.push((csp){x,t}); while(!q.empty()){ x=q.front().x; t=q.front().t; q.pop(); if(vis[x]) continue; vis[x]=1; if(x!=1) ans++; if(d<=t) continue; for(int i=0;i<a[x].size();i++) q.push((csp){a[x][i],t+1}); } return ans; } int main(){ n=read(),d=read(); for(int i=1;i<n;i++){ int u=read(),v=read(); a[u].push_back(v); a[v].push_back(u); } write(bfs(1,0)); return 0; } ```
by danlao @ 2024-02-24 14:37:08


是不是忘记把1号置为1了
by congege @ 2024-04-15 22:31:19


|