当前位置: 首页 > news >正文

【有向图 拓扑排序 】P8405 [COCI 2021/2022 #6] Naboj|普及+

本文涉及知识点

C++图论 拓扑排序

P8405 [COCI 2021/2022 #6] Naboj

题目描述

Šikić 先生是一位化学老师,他正在用 n n n 个金属球和 m m m 根铜导线做实验。他将一些对金属球用导线相连,使这些球(直接或间接地)与其他球都相连。他想要教学生关于电荷的知识,因此他将通过按顺序给金属球充电来演示它。

Šikić 先生可以让每个球带上正负电荷的一种。当一个金属球带负电荷时,与这个金属球相连的所有导线中的电子都将被排斥到与该导线相连的另一个金属球上。反过来,当一个金属球带正电荷时,与这个金属球相连的所有导线中的电子都将受这个金属球的吸引。无论导线的先前状态如何,给球充电对导线的影响都相同。

在刚上课的时候,所有的金属球都不带电,导线中的电子不动。对于每根导线,Šikić 先生都想让它其中电子的流向是某一个确定的方向。请帮他确定一个给金属球充电的顺序,使得最后电子的流向是他想要的。

输入格式

第一行两个数 n n n m m m,意义如题目描述。

接下来 m m m 行每行两个整数 a i a_i ai b i b_i bi,表示金属球 a i a_i ai b i b_i bi 之间有一根导线相连,并且这根导线中的电子应更靠近 a i a_i ai 而不是 b i b_i bi。在一对金属球之间最多只有一条导线。所有的金属球都通过导线直接或间接相连。

输出格式

如果不可能让最后电子的流向是 Šikić 先生想要的,输出 − 1 -1 1。否则输出 k k k,表示需要充电的金属球数量。 k k k 必须小于等于 200000 200000 200000

接下来 k k k 行,每行输出两个整数 c i c_i ci d i ( 1 ≤ c i ≤ n , 0 ≤ d i ≤ 1 ) d_i(1 \leq c_i \leq n,0 \leq d_i \leq 1) di(1cin,0di1),分别表示第 i i i 步 Šikić 先生需要充电的金属球的编号和是给这个金属球充正电荷(用 d i = 1 d_i=1 di=1 表示)还是负电荷(用 d i = 0 d_i=0 di=0 表示)。如果有多种方案,输出其中一种即可。

输入输出样例 #1

输入 #1

3 3
1 2
2 3
1 3

输出 #1

3
2 1
3 0
1 1

输入输出样例 #2

输入 #2

4 3
1 2
3 2
2 4

输出 #2

4
2 1
4 0
3 1
1 1

输入输出样例 #3

输入 #3

5 10
2 4
3 4
1 4
4 5
3 2
2 1
5 2
1 3
5 3
1 5

输出 #3

-1

说明/提示

样例解释 1:

首先,我们给金属球 2 2 2 充正电荷。金属球 1 , 2 1,2 1,2 和金属球 2 , 3 2,3 2,3 之间的导线中的电子现在更靠近 2 2 2。金属球 1 , 3 1,3 1,3 之间的导线仍保持中性。

现在我们给金属球 3 3 3 充负电荷。金属球 2 , 3 2,3 2,3 之间的导线状态不变,金属球 1 , 3 1,3 1,3 之间的导线中的电子更靠近金属球 1 1 1

最后我们给金属球 1 1 1 冲正电荷。金属球 1 , 3 1,3 1,3 之间的导线状态不变,但金属球 1 , 2 1,2 1,2 之间的导线现在将更靠近金属球 1 1 1,目标流向达成。

数据范围:

对于全部数据, 1 ≤ n ≤ 200000 1 \le n\le 200000 1n200000 1 ≤ m ≤ 500000 1\le m\le 500000 1m500000 1 ≤ a i , b i ≤ n , a i ≠ b i 1 \le a_i,b_ i\le n,a_i\neq b_i 1ai,bin,ai=bi

本题分值与 COCI 2021-2022#6 分值相同,满分 110 110 110

P8405 有向图 拓扑排序

本题不好理解,改成等效题:m条有向边,每条边都有一个哨兵,初始在边中间。对端点cur下命令1,和cur相连的边的哨兵会到达cur;对cur下命令0,cur相连的边的哨兵会到达边的另一端点。是否存在方案,使得所有边都在有向边的起点。
如果存在环,则一定无解。无论如何操作,环上一点的两个哨兵,要么都在cur;要么都不在cur;不可能一个靠近,一个远离。
如果无环,按top序(先叶子节点),对所有节点下达命令1。叶子节点的命令,可以省略。如果有节点,没有拓扑序,则说明有环。

代码

过不了大部分用例。3份题解,1分编译不过,2份和我一样的错误。
怀疑题目有问题。我和过了的网友沟通试试。有大佬给了我AC代码,也过不了。大佬试验了原题,可以AC。

核心代码

#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 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 CNeiBo
{
public:static vector<vector<int>> Two(int n, const vector<pair<int, int>>& edges, bool bDirect, int iBase = 0){vector<vector<int>>  vNeiBo(n);for (const auto& [i1, i2] : edges){vNeiBo[i1 - iBase].emplace_back(i2 - iBase);if (!bDirect){vNeiBo[i2 - iBase].emplace_back(i1 - iBase);}}return vNeiBo;}static vector<vector<int>> Two(int n, const vector<vector<int>>& edges, bool bDirect, int iBase = 0){vector<vector<int>>  vNeiBo(n);for (const auto& v : edges){vNeiBo[v[0] - iBase].emplace_back(v[1] - iBase);if (!bDirect){vNeiBo[v[1] - iBase].emplace_back(v[0] - iBase);}}return vNeiBo;}static vector<vector<std::pair<int, int>>> Three(int n, vector<vector<int>>& edges, bool bDirect, int iBase = 0){vector<vector<std::pair<int, int>>> vNeiBo(n);for (const auto& v : edges){vNeiBo[v[0] - iBase].emplace_back(v[1] - iBase, v[2]);if (!bDirect){vNeiBo[v[1] - iBase].emplace_back(v[0] - iBase, v[2]);}}return vNeiBo;}static vector<vector<std::pair<int, int>>> Three(int n, const vector<tuple<int, int, int>>& edges, bool bDirect, int iBase = 0){vector<vector<std::pair<int, int>>> vNeiBo(n);for (const auto& [u, v, w] : edges){vNeiBo[u - iBase].emplace_back(v - iBase, w);if (!bDirect){vNeiBo[v - iBase].emplace_back(u - iBase, w);}}return vNeiBo;}static vector<vector<int>> Mat(vector<vector<int>>& neiBoMat){vector<vector<int>> neiBo(neiBoMat.size());for (int i = 0; i < neiBoMat.size(); i++){for (int j = i + 1; j < neiBoMat.size(); j++){if (neiBoMat[i][j]){neiBo[i].emplace_back(j);neiBo[j].emplace_back(i);}}}return neiBo;}
};class CDGTopSort
{
public:template <class T = vector<int> >CDGTopSort(const vector<T>& vNeiBo) :m_vDeg(vNeiBo.size()) {const int N = vNeiBo.size();m_backNeiBo.resize(N);for (int cur = 0; cur < N; cur++){m_vDeg[cur] = vNeiBo[cur].size();for (const auto& next : vNeiBo[cur]){m_backNeiBo[next].emplace_back(cur);}}}void Init() {auto Add = [&](int i) {if (0 != m_vDeg[i]) { return; }m_que.emplace(i);};for (int i = 0; i < m_vDeg.size(); i++){Add(i);}while (m_que.size()){const int cur = m_que.front(); m_que.pop();if (!OnDo(cur)) { continue; }for (const auto& next : m_backNeiBo[cur]){m_vDeg[next]--;Add(next);}};}queue<int> m_que;vector<int> m_vDeg;
protected:vector<vector<int>> m_backNeiBo;virtual bool OnDo(int cur) { return true; };
};class CMyTopSort : public CDGTopSort
{
public:CMyTopSort(const vector<vector<int>>& vNeiBo) : CDGTopSort(vNeiBo) {}vector<int> m_ans;
protected:virtual bool OnDo(int cur) {m_ans.emplace_back(cur);return true;};
};
class Solution {
public:vector<int> Ans(const int N, vector<pair<int, int>>& edge) {auto neiBo = CNeiBo::Two(N, edge, true, 1);CMyTopSort topSort(neiBo);topSort.Init();return topSort.m_ans;}
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	//ios::sync_with_stdio(0); cin.tie(nullptr);CInBuff<> in; COutBuff<10'000'000> ob;	int N;cin >> N;auto edge = in.Read<pair<int, int>>();
#ifdef _DEBUG		printf("N=%d", N);Out(edge, "edge=");//Out(que, "que=");//Out(B, "B=");//Out(que, "que=");//Out(B, "B=");
#endif // DEBUG	auto res = Solution().Ans(N,edge);if (res.size() < N) {cout << -1;}else {cout << res.size() << "\n";for (const auto& i : res){cout << i +1<< " 1\n";}}		return 0;
}

单元测试

	int N;vector<pair<int, int>> edge;TEST_METHOD(TestMethod11){N = 3,edge = { {1,2},{2,3},{1,3} };auto res = Solution().Ans(N, edge);AssertV({2,1,0 }, res);}TEST_METHOD(TestMethod12){N = 4, edge = { {1,2},{3,2},{2,4} };auto res = Solution().Ans(N, edge);AssertV({3,1,0,2 }, res);}TEST_METHOD(TestMethod13){N = 5,edge = { {2,4},{3,4},{1,4},{4,5},{3,2},{2,1},{5,2},{1,3},{5,3},{1,5} };auto res = Solution().Ans(N, edge);AssertV({ }, 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++**实现。

http://www.xdnf.cn/news/738721.html

相关文章:

  • 为什么arc中,(cons ‘a (cons 1 (cons “foo“ ‘(b) ))) 是(a 1 “foo“ b)
  • 使用函数证明给定的三个数是否能构成三角形
  • 偏序集、哈斯图、Dilworth
  • 如何做好一份技术文档
  • java25
  • python笔面试题汇总
  • 如何选择合适的培养基过滤器
  • python打卡训练营打卡记录day40
  • 案例分享--血管支架的径向力分布评估--DIC数字图像相关技术用于生物医学-高置信度DIC测量
  • 拉深工艺模块——回转体拉深件毛坯尺寸的确定(一)
  • 初探Linux内核:解锁Linux操作系统的基本核心的奥秘(二)
  • Prevent this information from being displayed to the user 修复方案
  • 涨薪技术|0到1学会性能测试第91课-性能测试过程执行、分析、诊断、调节
  • ASR、TTS与语音克隆技术简介
  • QML 滑动与翻转效果(Flickable与Flipable)
  • 小狼毫输入法雾凇拼音输入方案辅码由默认的部件拆字/拼音输入方案修改为五笔画方案
  • 书送希望 智启未来 —— 赛力斯超级工厂携手渝北和合家园小学校开展公益赠书活动
  • JavaSwing之--JPasswordField
  • 系统设计——状态机模型设计经验
  • Linux ClearOS yum无法使用解决备忘
  • Qt Dial(旋钮)
  • 智慧赋能充电桩管理:我国新能源充电桩建设现状与突破路径
  • 【Doris基础】Apache Doris业务场景全解析:从实时数仓到OLAP分析的完美选择
  • Linux操作系统 使用共享内存实现进程通信和同步
  • 近期手上的一个基于Function Grap(类AWS的Lambda)小项目的改造引发的思考
  • URAT接收实验日志,传输无效
  • 第29次CCF计算机软件能力认证-2-垦田计划
  • espefuse.py烧录MAC地址
  • leetcode1201. 丑数 III -medium
  • (23)JNI 内存泄漏诊断