本文涉及知识点
P9661 [ICPC 2021 Macao R] Sandpile on Clique
题目描述
阿贝尔沙堆模型(Abelian Sandpile Model)是一个著名的显示自组织临界性的动力学系统。自从它由 Per Bak、Chao Tang 和 Kurt Wiesenfeld 在 1987 年的一篇论文中引入以来,它已经被研究了数十年。沙堆模型的预测引起了物理学、计算机科学和数学的广泛关注,这不仅是因为它美丽的代数结构,还因为它与负载平衡和内部扩散有关的模型的应用,如去随机化。沙堆模型与许多其他模型和物理现象相关,如转子路由模型、雪崩模型。
在沙堆模型中,给定一个顶点编号从 1 1 1 到 n n n 的无向图 G G G。我们还给出了 n n n 个整数 a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,⋯,an,其中 a i a_i ai 表示初始时放置在顶点 i i i 上的筹码数量。每个回合,我们将选择一个任意的顶点 v v v,使得 v v v 上的筹码数量不小于与 v v v 相连的边数,记为 d v d_v dv。对于 v v v 的每个邻居,它将从 v v v 接收一枚筹码。因此, v v v 将失去 d v d_v dv 枚筹码。这个过程被称为 firing 或 toppling。直到没有顶点 v v v 至少有 d v d_v dv 枚筹码时,firing 才会停止。
可以证明,firing 的顺序不会影响结果。同时,也可能 firing 永远不会终止。这种情况被描述为"recurrent"。现在给定一个团和初始筹码数量,请确定这个实例是否是一个 recurrent 实例。如果不是,请分别输出每个节点的最终筹码数量。
团(也称为完全图)是一个图,其中任意两个顶点都有边相连。
输入格式
每个测试文件中只有一个测试用例。
输入的第一行包含一个整数 n n n( 2 ≤ n ≤ 5 × 10 5 2 \leq n \leq 5 \times 10^5 2≤n≤5×105),表示团的大小。
第二行包含 n n n 个整数 a 1 , a 2 , ⋯ , a n a_1, a_2, \cdots, a_n a1,a2,⋯,an( 0 ≤ a i ≤ 10 9 0 \leq a_i \leq 10^9 0≤ai≤109),其中 a i a_i ai 表示放置在顶点 i i i 上的初始筹码数量。
输出格式
输出一行。如果给定的沙堆实例将终止,则输出由空格分隔的 n n n 个整数,其中第 i i i 个整数表示第 i i i 个顶点上的最终筹码数量。否则输出 Recurrent(不包括引号)。
请不要在每行末尾输出额外的空格,否则您的解决方案可能被认为是错误的!
【样例解释】
对于第一个样例测试用例:
- 我们只能在开始时选择顶点 1 1 1。筹码数量变为 { 1 , 1 , 4 , 1 , 4 } \{1, 1, 4, 1, 4\} {1,1,4,1,4}。
- 现在我们可以选择顶点 3 3 3 或 5 5 5,因为它们都至少有 4 4 4 枚筹码。我们选择顶点 3 3 3,筹码数量变为 { 2 , 2 , 0 , 2 , 5 } \{2, 2, 0, 2, 5\} {2,2,0,2,5}。选择顶点 5 5 5 会得到相同的结果。
- 现在我们选择顶点 5 5 5。筹码数量变为 { 3 , 3 , 1 , 3 , 1 } \{3, 3, 1, 3, 1\} {3,3,1,3,1}。没有顶点至少有 4 4 4 枚筹码,因此 firing 终止。
对于第二个样例测试用例,我们可以重复选择顶点 1 1 1 和 2 2 2。firing 永远不会终止。
翻译来自于:ChatGPT
输入输出样例 #1
输入 #1
5
5 0 3 0 3
输出 #1
3 3 1 3 1
输入输出样例 #2
输入 #2
2
1 0
输出 #2
Recurrent
图论
性质一 :如果连续n-1个操作是不同的节点派发筹码。则可以无限循环。此是没有派发筹码的节点n1至少收到n-1个筹码。n1派发后,仍然符合性质一。
性质二 :如果能派发筹码n-1次,则可以无限循环。假定x个节点能派发n-1次,调整派发顺序,让后x次没有相同的派发节点。余下的x-1个节点,都至少有n-1筹码。
依次派发符合性质一。
大根堆记录各节点筹码数量和下标,cnt+堆中数量才是真实数量。x = 堆顶元素,如果x <n-1结束,返回ans。否则出堆 x-=n,入堆.cnt++。
循环n-1次后,返回无限;否则,返回ans[堆中元素下标] = 堆中元素值+cnt。
代码
核心代码
cpp
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4,T5,T6,T7>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class Solution {
public:
vector<int> Ans(vector<int>& a) {
const int N = a.size();
priority_queue<pair<int, int>> maxHeap;
for (int i = 0; i < N; i++) {
maxHeap.emplace(a[i], i);
}
int cnt = 0;
for (int i = 0; i < N - 1; i++) {
if (maxHeap.top().first + cnt < N - 1) {
vector<int> ans(N);
while (maxHeap.size()) {
ans[maxHeap.top().second] = cnt + maxHeap.top().first;
maxHeap.pop();
}
return ans;
}
auto [c, inx] = maxHeap.top(); maxHeap.pop();
c -= N;
cnt++;
maxHeap.emplace(c, inx);
}
return {};
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
auto a = Read<int>();
#ifdef _DEBUG
//printf("N=%d,K=%d", N,K);
//Out(ws, ",ws=");
Out(a, ",a=");
#endif // DEBUG
auto res = Solution().Ans(a);
if (res.empty()) {
cout << "Recurrent" << "\n";
}
else {
for (const auto& i : res) {
cout << i << " ";
}
}
return 0;
};
单元测试
cpp
vector<int> a;
TEST_METHOD(TestMethod11)
{
a = { 5,0,3,0,3 };
auto res = Solution().Ans(a);
AssertEx({ 3,3,1,3,1 },res);
}
TEST_METHOD(TestMethod12)
{
a = { 1,0 };
auto res = Solution().Ans(a);
AssertEx({ }, res);
}

扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法 用**C++**实现。
