이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 300;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9;
int m, n, t, b[N], d[N], lab[N], q;
int k, a[N];
pii dp[N][M];
int ans, tong;
vector<int> adj[N], g[N];
vector<int> vi;
string s[N];
bool vis[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void add(int &x, int y)
{
x += y;
if(x >= mod)x -= mod;
}
int findp(int u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(int u, int v)
{
u = findp(u);
v = findp(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
}
bool cmp(const int& x, const int& y)
{
return d[x] > d[y];
}
void sol()
{
cin >> n >> m >> q;
for(int i = 1; i <= m; i ++)
{
int x, y;
cin >> x >> y;
adj[y].pb(x);
g[x].pb(y);
}
for(int i = 1; i <= n; i ++)for(int j = 0; j < M; j ++)dp[i][j].fi = -1;
for(int u = 1; u <= n; u ++)
{
vis[u] = true;
vi.pb(u);
for(int v: adj[u])
{
for(int j = 0; j < M; j ++)
{
if(dp[v][j].fi == -1)break;
d[dp[v][j].se] = max(d[dp[v][j].se], dp[v][j].fi+1);
if(!vis[dp[v][j].se])
{
vis[dp[v][j].se] = true;
vi.pb(dp[v][j].se);
}
}
}
sort(vi.begin(), vi.end(), cmp);
for(int j = 0; j < min((int)vi.size(), M); j ++)
{
dp[u][j].fi = d[vi[j]];
dp[u][j].se = vi[j];
}
for(int j = 0; j < (int)vi.size(); j ++)
{
vis[vi[j]] = false;
d[vi[j]] = 0;
}
}
while(q -- > 0)
{
cin >> k >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
vis[a[i]] = true;
}
if(dp[k][0].fi == -1)cout << -1 << '\n';
else
{
ans = -1;
if(n >= M)
{
for(int u = 1; u <= k; u ++)
{
if(vis[u])d[u] = -1;
else d[u] = 0;
}
for(int u = 1; u <= k; u ++)
{
if(d[u] == -1)continue;
for(int v: g[u])d[v] = max(d[v], d[u]+1);
}
ans = d[k];
}
else
{
for(int i = 0; i < M; i ++)
{
if(!vis[dp[k][i].se])
{
ans = dp[k][i].fi;
break;
}
}
}
cout << ans << '\n';
}
for(int i = 1; i <= n; i ++)vis[a[i]] = false;
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bitaro.cpp: In function 'int main()':
bitaro.cpp:147:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:148:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |