이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define endl '\n'
#define ll int
using namespace std;
const int MAXN = 3e5 + 10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, m, q;
int bracket;
vector < int > g[MAXN], grev[MAXN];
int tt, yy;
void read()
{
cin >> n >> m >> q;
bracket = sqrt(n);
int from, to;
for (int i = 1; i <= m; ++ i)
{
cin >> from >> to;
g[from].push_back(to);
grev[to].push_back(from);
}
}
int marked[MAXN], dp[MAXN];
deque < pair < int, int > > opt[MAXN];
int c[MAXN], f[MAXN];
int used[MAXN];
deque < pair < int, int > > comb(deque < pair < int, int > > f, deque < pair < int, int > > s)
{
vector < int > usedv;
deque < pair < int, int > > q;
while(f.size() && s.size() && q.size() <= bracket)
{
pair <int, int > ft = f.front();
pair < int, int > st = s.front();
st.first ++;
if(ft.first > st.first)
{
f.pop_front();
if(!used[ft.second])
{
used[ft.second] = 1;
usedv.push_back(ft.second);
q.push_back(ft);
}
}
else
{
s.pop_front();
if(!used[st.second])
{
used[st.second] = 1;
usedv.push_back(st.second);
q.push_back(st);
}
}
}
while(f.size() && q.size() <= bracket)
{
pair <int, int > ft = f.front();
f.pop_front();
if(!used[ft.second])
{
used[ft.second] = 1;
usedv.push_back(ft.second);
q.push_back(ft);
}
}
while(s.size() && q.size() <= bracket)
{
pair <int, int > st = s.front();
st.first ++;
s.pop_front();
if(!used[st.second])
{
used[st.second] = 1;
usedv.push_back(st.second);
q.push_back(st);
}
}
for (int i = 0; i < usedv.size(); ++ i)
used[usedv[i]] = 0;
return q;
}
void precompute_small()
{
for (int i = 1; i <= n; ++ i)
opt[i].clear();
for (int i = 1; i <= n; ++ i)
{
int apar;
deque < pair < int, int > > qq;
qq.push_back(make_pair(0, i));
opt[i] = qq;
for (int j = 0; j < grev[i].size(); ++ j)
{
apar = grev[i][j];
opt[i] = comb(opt[i], opt[apar]);
}
}
/*for (int i =1;i <= n; ++ i)
{
cout << "for i " << i << endl;
for (int j = 0; j < opt[i].size(); ++ j)
cout << " (" <<opt[i][j].first << "," << opt[i][j].second <<") ";
cout <<endl;
}*/
}
void solve_small()
{
// cout << "is small" << endl;
int maxx = -1;
for (int i = 0; i < opt[tt].size(); ++ i)
{
if(!marked[opt[tt][i].second])
maxx = max(maxx, opt[tt][i].first);
}
cout << maxx << endl;
}
void solve_big()
{
//cout << "is big" << endl;
for (int i = 1; i <= n; ++ i)
dp[i] = -1;
int maxx = -1, cnt = 0;
for (int i = tt; i >= 1; -- i)
{
if(i == tt)
{
dp[i] = 0;
if(!marked[i])
maxx = 0;
continue;
}
for (int j = 0; j < g[i].size(); ++ j)
{
int nb = g[i][j];
if(dp[nb] != -1)dp[i] = max(dp[i], dp[nb] + 1);
}
/// cout << dp[i] << endl;
if(!marked[i])maxx = max(maxx, dp[i]);
}
cout << maxx << endl;
}
int main()
{
speed();
read();
precompute_small();
while(q --)
{
cin >> tt >> yy;
for (int i = 1; i <= yy; ++ i)
{
cin >> c[i];
marked[c[i]] = 1;
}
if(yy > bracket)
{
solve_big();
}
else solve_small();
for (int i = 1; i <= yy; ++ i)
{
marked[c[i]] = 0;
}
}
return 0;
}
/***
12 17 1
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 6
6 7
7 8
5 9
6 10
7 11
8 12
9 10
10 11
11 12
6 3 1 7 12
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
10 1 3
1 2
1 0
2 1 2
3 1 2
/*/
컴파일 시 표준 에러 (stderr) 메시지
bitaro.cpp: In function 'std::deque<std::pair<int, int> > comb(std::deque<std::pair<int, int> >, std::deque<std::pair<int, int> >)':
bitaro.cpp:38:44: warning: comparison of integer expressions of different signedness: 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | while(f.size() && s.size() && q.size() <= bracket)
| ~~~~~~~~~^~~~~~~~~~
bitaro.cpp:65:32: warning: comparison of integer expressions of different signedness: 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
65 | while(f.size() && q.size() <= bracket)
| ~~~~~~~~~^~~~~~~~~~
bitaro.cpp:76:32: warning: comparison of integer expressions of different signedness: 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
76 | while(s.size() && q.size() <= bracket)
| ~~~~~~~~~^~~~~~~~~~
bitaro.cpp:88:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for (int i = 0; i < usedv.size(); ++ i)
| ~~^~~~~~~~~~~~~~
bitaro.cpp: In function 'void precompute_small()':
bitaro.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int j = 0; j < grev[i].size(); ++ j)
| ~~^~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void solve_small()':
bitaro.cpp:122:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for (int i = 0; i < opt[tt].size(); ++ i)
| ~~^~~~~~~~~~~~~~~~
bitaro.cpp: In function 'void solve_big()':
bitaro.cpp:145:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for (int j = 0; j < g[i].size(); ++ j)
| ~~^~~~~~~~~~~~~
bitaro.cpp:135:20: warning: unused variable 'cnt' [-Wunused-variable]
135 | int maxx = -1, 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... |