Problem C Book 1573, can somebody gimve me a taest which won`t work
Difference between ru1 and ru2, changed 2,160 character(s)

#include<bits/stdc++.h>↵
#include<unordered_map>↵

#define int1 LL ↵
#define FOR(i,n) for (int i = 0; i < n; ++i)↵
#define FORN(i, a, b) for (int i = a; i < b; ++i)↵
#define FORE(i, a, b) for (int i = a; i <= b; ++i)↵
#define DOWN(i,n) for (int i = n &mdash; 1; i >= 0; --i)↵
#define SQR(x) ((x)*(x))↵
#define ALL(c) (c).begin(), (c).end()↵
//#define map make_pair↵
#define pb push_back↵
#define fi first↵
#define se second↵
#define LL long long ↵
//typedef pair <int, int> PII;↵
//typedef pair <LL, LL> PLL;↵
//typedef vector<int> VI;↵
//typedef vector<VI> VVI;↵
//typedef vector<LL> VL;↵
//typedef vector<VL> VVL;↵
//typedef vector<PLL> VLP;↵
using namespace std;↵
typedef long long ll;↵
typedef long double LDB;↵
typedef float doubl;↵
typedef unsigned long long ULL;↵
const ll INF = 2e9;↵
const ll N = 400 + 1, K = 1e9 + 7;↵
vector<vector<ll>>g, ginv;↵
vector<ll>u;↵
bool has = 0;↵
void dfs(ll v) {↵
if (has)return;↵
u[v] = 1;↵
for (auto to : g[v]) {↵
if (u[to] == 1) {↵
has = 1;↵
return;↵
}↵
if(u[to] == 0)dfs(to);↵
}↵
u[v] = 2;↵
}↵
int main()↵
{↵
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);↵
ll t;↵
cin >> t;↵
while (t--) {↵
ll n;↵
cin >> n;↵
has = 0;↵
g.clear();↵
ginv.clear();↵
u.clear();↵
g.resize(n);↵
ginv.resize(n);↵
u.resize(n);↵
vector<ll>w(n);↵
fill(ALL(u), 0);↵
queue<ll>q;↵
set<ll>s;↵
for (int i = 0;i < n;++i) {↵
ll k;↵
cin >> k;↵
w[i] = k;↵
if (k == 0) {↵
s.insert(i);↵
}↵
for (int j = 0;j < k;++j) {↵
ll a;↵
cin >> a;↵
a--;↵
g[i].push_back(a);↵
ginv[a].push_back(i);↵
}↵
}↵
for (int i = 0;i < n;++i) {↵
if (u[i] == 0) {↵
dfs(i);↵
}↵
}↵
if (has == 1) {↵
cout << "-1\n";↵
continue;↵
}↵
ll res = 0;↵
do {↵
while (!q.empty()) {↵
ll a = q.front();↵
q.pop();↵
s.insert(a);↵
}↵
while (!s.empty()) {↵
auto a = *(--s.end());↵
s.erase(a);↵
for (auto to : ginv[a]) {↵
w[to]--;↵
if (w[to] == 0 && to > a) {↵
s.insert(to);↵
}↵
else if (w[to] == 0) {↵
q.push(to);↵
}↵
}↵
}↵
res++;↵
} while (!q.empty());↵
cout << res << "\n";↵
}↵
}↵
/*↵

*/
https://mirror.codeforces.com/contest/1573/submission/130580243

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
ru2 Russian lavanda1 2021-10-02 19:29:15 2160
ru1 Russian lavanda1 2021-10-02 19:28:15 2245 Первая редакция (опубликовано)