# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
35002 | long10024070 | 스파이 (JOI13_spy) | C++11 | 2000 ms | 33500 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define Link ""
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define TASK "SPY"
using namespace std;
void OpenFile()
{
freopen(".INP","r",stdin);
freopen(".OUT","w",stdout);
}
const int maxn = 2e3 + 1;
int n,m,p[2][maxn],res[maxn],c[2][maxn][maxn],cnt[2][maxn];
int FastIn()
{
int res = 0;
register char c = getchar();
while (c < '0' || '9' < c) c = getchar();
while ('0' <= c && c <= '9') res = res * 10 + c - '0', c = getchar();
return res;
}
inline void writeln(int x)
{
char buffor[21];
register int i=0;
int neg=0;
if (x<0) {
neg=1;
x= -x;
}
do {
buffor[i++]=(x%10)+'0';
x/=10;
}
while(x);
i--;
if (neg) putchar('-');
while(i>=0) putchar(buffor[i--]);
putchar('\n');
}
void Enter()
{
n = FastIn();
m = FastIn();
for (int i=1;i<=n;++i) {
p[0][i] = FastIn();
p[1][i] = FastIn();
}
}
void DFS(int t, int i, int u)
{
if (u == 0)
return;
c[t][u][cnt[t][u]++] = i;
DFS(t,i,p[t][u]);
}
void Init()
{
for (int i=1;i<=n;++i) {
DFS(0,i,i);
DFS(1,i,i);
}
for (int i=1;i<=n;++i) {
sort(c[0][i],c[0][i]+cnt[0][i]);
sort(c[1][i],c[1][i]+cnt[1][i]);
}
}
void Solve()
{
for (;m>0;--m) {
int R,S;
R = FastIn();
S = FastIn();
int i = 0;
int j = 0;
while (c[0][R][i] != 0 && c[1][S][j] != 0)
if (c[0][R][i] == c[1][S][j]) {
++res[c[1][S][j]];
++i;
++j;
}
else
if (c[0][R][i] < c[1][S][j])
++i;
else
++j;
}
for (int i=1;i<=n;++i)
writeln(res[i]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//OpenFile();
Enter();
Init();
Solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |