E - Transitivity
E - Transitivity
https://atcoder.jp/contests/abc292/tasks/abc292_e
思路
https://blog.csdn.net/a1845613403/article/details/129343684
记录每个点的入节点集合, 和 出节点集合,
对每个节点, 统计每一条入线 和 出线组合, 是否要添加入节点和出节点的连线。
需要注意的是, 在添加的过程中,产生的边,也需要考虑到判断条件中,
例如
初始状态, 2 3 1 不满足if条件, 即存在两个边的有向转移关系,
但是经过2 -> 4 -> 3 条件判断后, 添加了一个边 2-> 3 之后,
2 3 1 满足if条件
2 -> 3 -> 1
Code
#include <iomanip> #include <bits/stdc++.h> #include <iostream> using namespace std; #include <limits.h> #include <math.h> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #define pb push_back const int N = 2005; int g[N][N]; int n, m; void solve() { vector<vector<int>> in(n + 1), out(n + 1); while(m--) { int x, y; cin >> x >> y; out[x].pb(y); in[y].pb(x); g[x][y] ++; } int ans = 0; for(int i = 1; i <= n; ++i) { auto din = in[i], dout = out[i]; for(auto x : din) for(auto y : dout) { if(x == y) continue; if(!g[x][y]) { ++ans; g[x][y] ++; in[y].pb(x); out[x].pb(y); } } } cout << ans << endl; } int main() { cin >> n >> m; solve(); return 0; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。