2 条题解

  • 0
    @ 2025-10-8 16:58:29
    #include<bits/stdc++.h>
    using namespace std;
    const int N =
    • 0
      @ 2025-10-8 16:58:07
      #include<bits/stdc++.h>
      using namespace std;
      const int N = 2e5+10; 
      vector<int>G[N];
      int n,k,res[N],ans;//res:标记该子树是否有剩余节点未连接 
      void dfs(int x,int xfa)//遍历 
      {
      	for(int y:G[x])if(y!=xfa)
      	{
      		dfs(y,x);
              res[x]+=res[y];
      	}
          res[x]=min(res[x],2*k);
          ans+=res[x]/2,res[x]%=2;
      }
      int main()
      {
      	scanf("%d%d",&n,&k);
      	for(int i=1,x,y;i<=n-1;i++)
      	{
      		scanf("%d%d",&x,&y);
      		G[x].push_back(y);
              G[y].push_back(x);
      	}
          memset(res,0,sizeof(res));
          for(int i=1;i<=n;i++) if(G[i].size()==1)res[i]=1;
          ans=0;
          dfs(1,0);
      	printf("%d\n",ans);
      	return 0;
      }
      • 1

      *【贪心】树上统计[USACO10JAN] Cow Telephones G

      信息

      ID
      1719
      时间
      1000ms
      内存
      128MiB
      难度
      4
      标签
      递交数
      39
      已通过
      21
      上传者