problemscpp
A collection of my answers to algorithm problems in c++.
| 函数
acwing::acwing240 命名空间参考

  1. 食物链
更多...

struct  UnionFind
 

函数

int main (istream &cin, ostream &cout)
 
 TEST (acwing240, case1)
 

详细描述

  1. 食物链

函数说明

◆ main()

int acwing::acwing240::main ( istream &  cin,
ostream &  cout 
)

在文件 acwing.cpp7031 行定义.

7031 {
7032 int n;
7033 int k;
7034 cin >> n >> k;
7035 UnionFind uf(n);
7036 int ans = 0;
7037 while(k-- != 0) {
7038 char d;
7039 int x;
7040 int y;
7041 cin >> d >> x >> y;
7042 if(x > n || y > n) {
7043 ans++;
7044 continue;
7045 }
7046 x--;
7047 y--;
7048 const int px = uf.find(x);
7049 const int py = uf.find(y);
7050 if(d == '1') {
7051 if(px == py) {
7052 if((uf.dist[x] - uf.dist[y]) % 3 != 0) {
7053 ans++;
7054 }
7055 } else {
7056 uf.parent[px] = py;
7057 uf.dist[px] = uf.dist[y] - uf.dist[x];
7058 }
7059 } else {
7060 if(x == y) {
7061 ans++;
7062 continue;
7063 }
7064 if(px == py) {
7065 if((uf.dist[x] - uf.dist[y] - 1) % 3 != 0) {
7066 ans++;
7067 }
7068 } else {
7069 uf.parent[px] = py;
7070 uf.dist[px] = uf.dist[y] - uf.dist[x] + 1;
7071 }
7072 }
7073 }
7074 cout << ans;
7075 return 0;
7076 }

引用了 acwing::acwing240::UnionFind::dist, acwing::acwing240::UnionFind::find() , 以及 acwing::acwing240::UnionFind::parent.

被这些函数引用 TEST().

◆ TEST()

acwing::acwing240::TEST ( acwing240  ,
case1   
)

在文件 acwing_test.cpp3292 行定义.

3292 {
3293 istringstream in("100 7\n"
3294 "1 101 1 \n"
3295 "2 1 2\n"
3296 "2 2 3 \n"
3297 "2 3 3 \n"
3298 "1 1 3 \n"
3299 "2 3 1 \n"
3300 "1 5 5");
3301 auto out = ostringstream();
3302 main(in, out);
3303 const auto ans = out.str();
3304 ASSERT_EQ("3", ans);
3305 }
int main(int argc, char **argv)
Definition: main.cpp:5

引用了 main().