This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*****from dust I have come, dust I will be*****/
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define forn(i,n) for(int i=0;i<n;i++)
int dx[8] = { 1, 0, -1, 0, -1, 1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, -1, 1, 1, -1 };
int ceil_div(int a, int b) {return a % b == 0 ? a / b : a / b + 1;}
const int MOD = 1000000007;
//const int MOD = 998244353;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
/*
int k = A.order_of_key(p[i].second);
A.erase(A.find_by_order(k));
erase element from pbds multiset
*/
void solve(){
int n; cin >> n;
vector<set<int>>g(n);
multiset<pair<int, int>, greater<pair<int, int>>>skup;
forn(i,n){
int sz; cin >> sz;
int x;
forn(j,sz){
cin >> x;
g[x-1].insert(i);
}
}
int mx = -1, index = 0;
forn(i,n){
if (g[i].size() > mx){
mx = g[i].size();
index = i;
}
}
vector<vector<int>>tree(n), rtree(n);
vector<int>sum(n);
int cnt = 0;
skup.insert({mx, index});
set<int>vis;
while (!skup.empty()){
auto mx = *skup.begin();
if (mx.first == 0)break;
for (auto t : skup){
forn(i,n){
if (vis.find(i) != vis.end()) continue;
if (g[i].find(t.second) != g[i].end()){
g[i].erase(g[i].find(t.second));
}
}
}
skup.erase(skup.begin());
vis.insert(mx.second);
for (auto x : g[mx.second]){
skup.insert({g[x].size(), x});
tree[mx.second].push_back(x);
}
}
stack<int>q;
forn(i,n){
if (tree[i].size() == 0){
q.push(i);
//sum[i] = 1;
}
for (auto g : tree[i]) rtree[g].push_back(i);
}
// forn(i,n){
// cout <<i+1<<" -> ";
// for (auto g : rtree[i]) cout <<g+1<<' ';
// cout <<'\n';
// }
while (!q.empty()){
auto t = q.top();
sum[t]++;
q.pop();
for (auto x : rtree[t]){
sum[x]++;
q.push(x);
}
}
int ans = 0;
for (auto t : sum) ans += t;
cout <<ans<<'\n';
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1; //cin >>t;
for (int i=1;i<=t;i++){
solve();
}
return 0;
}
Compilation message (stderr)
bosses.cpp: In function 'void solve()':
bosses.cpp:39:25: warning: comparison of integer expressions of different signedness: 'std::set<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
39 | if (g[i].size() > mx){
| ~~~~~~~~~~~~^~~~
bosses.cpp:46:9: warning: unused variable 'cnt' [-Wunused-variable]
46 | int cnt = 0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |