问题不难,就模拟一下,依次找到可以分配石头的节点,但是WA,不知道什么问题,还望同伴给帮解一下困惑
#include <cstdio>
include
include
using namespace std; vector stones; vector degree; vector> nodes; queue validNodes; int main(){ int N, M;//N:nodes,M:edges scanf("%d%d", &N, &M); stones = vector(N); degree = vector(N, 0); for (int i = 0; i()); scanf("%d", &stones[i]); }
while (M--){
int x, y;
scanf("%d%d", &x, &y);
nodes[x].push_back(y);
nodes[y].push_back(x);
degree[x]++;
degree[y]++;
}
for (int i = 0; i<N; i++){
if (degree[i] <= stones[i])
validNodes.push(i);
}
for (int i = 0; i<=100000; i++){
if (validNodes.empty()){
printf("%d", i);
return 0;
}
//find the valid node
int node = validNodes.front();
/**
待处理,不一定要pop()
validNodes.pop();
*/
//distribute stones to neighbors
for (int j = 0; j<degree[node]; j++){
//邻居+1,自己-1
int neigh = nodes[node][j];
stones[neigh]++;
stones[node]--;
if (stones[neigh] >= degree[neigh])
validNodes.push(neigh);
}
if (stones[node]<degree[node])
validNodes.pop();
}
printf("INF");
return 0;
}