problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::maximum_good_people_based_on_statements::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static pair< int, unordered_map< int, bool > > dfs (vector< vector< int > > &statements, unordered_map< int, bool > um, queue< msg > que)
 
static int maximumGood (vector< vector< int > > &statements)
 

详细描述

在文件 leetcode.h678 行定义.

成员函数说明

◆ dfs()

pair< int, unordered_map< int, bool > > leetcode::maximum_good_people_based_on_statements::Solution::dfs ( vector< vector< int > > &  statements,
unordered_map< int, bool >  um,
queue< msg que 
)
static

在文件 leetcode.cpp1528 行定义.

1528 {
1529 int maximum = 0;
1530 bool contradict = false;
1531 while(!que.empty()) {
1532 auto current = que.front();
1533 que.pop();
1534 if(um.contains(current.person)) {
1535 //已经存在
1536 if(um[current.person] != current.good) {
1537 //矛盾
1538 return pair<int, unordered_map<int, bool>>(0, {});
1539 }
1540 } else {
1541 um.insert(pair(current.person, current.good));
1542 }
1543 if(current.good) {
1544 //是好人
1545 for(int j = 0; j < statements.size(); j++) {
1546 if(statements[current.person][j] != 2 && j != current.person) {
1547 auto nmsg = msg(j, statements[current.person][j] == 1);
1548 if(um.contains(nmsg.person)) {
1549 //已经有了
1550 if(um[nmsg.person] != nmsg.good) {
1551 //矛盾
1552 contradict = true;
1553 return pair<int, unordered_map<int, bool>>(0, {});
1554 }
1555 } else {
1556 //还没有
1557 que.push(nmsg);
1558 }
1559 }
1560 }
1561 }
1562 }
1563 if(!contradict) {
1564 //不矛盾
1565 auto dup = unordered_map<int, bool>();
1566 for(int i = 0; i < statements.size(); i++) {
1567 if(dup[i]) {
1568 continue;
1569 }
1570 if(!um.contains(i)) {
1571 bool all2 = true;
1572 for(auto v: statements[i]) {
1573 if(v != 2) {
1574 all2 = false;
1575 }
1576 }
1577 if(all2) {
1578 um.insert(pair(i, true));
1579 continue;
1580 }
1581 auto nque = queue<msg>();
1582 nque.push(msg(i, true));
1583 auto ans = dfs(statements, um, nque);
1584 maximum = max(maximum, ans.first);
1585 for(auto it: ans.second) {
1586 if(it.second) {
1587 dup.insert(it);
1588 }
1589 }
1590 }
1591 }
1592
1593 int good_count = 0;
1594 for(auto i: um) {
1595 if(i.second) {
1596 good_count++;
1597 }
1598 }
1599 maximum = max(maximum, good_count);
1600 }
1601 return pair(maximum, um);
1602 }
static pair< int, unordered_map< int, bool > > dfs(vector< vector< int > > &statements, unordered_map< int, bool > um, queue< msg > que)
Definition: leetcode.cpp:1528

引用了 dfs().

被这些函数引用 dfs() , 以及 maximumGood().

◆ maximumGood()

int leetcode::maximum_good_people_based_on_statements::Solution::maximumGood ( vector< vector< int > > &  statements)
static

在文件 leetcode.cpp1507 行定义.

1507 {
1508 int maximum = 0;
1509 auto dup = unordered_map<int, bool>();
1510 for(int i = 0; i < statements.size(); i++) {
1511 if(dup[i]) {
1512 continue;
1513 }
1514 const auto um = unordered_map<int, bool>();
1515 auto que = queue<msg>();
1516 que.push(msg(i, true));
1517 auto ans = dfs(statements, um, que);
1518 maximum = max(maximum, ans.first);
1519 for(auto it: ans.second) {
1520 if(it.second) {
1521 dup.insert(it);
1522 }
1523 }
1524 }
1525 return maximum;
1526 }

引用了 dfs().

被这些函数引用 leetcode::maximum_good_people_based_on_statements::TEST().


该类的文档由以下文件生成: