이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <iostream>
#include <fstream>
#include <deque>
#include <algorithm>
#include <utility>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
#define in cin
#define out cout
#define PII pair<int, int>
#define x first
#define y second
#define pi 3.14159265358979323846
#define REP(i, v) for(int i=0; i<v; i++)
#define REPEQ(i, s, e) for(int i=s; i<=e; i++)
#define REPRV(i, s, e) for(int i=s; i>=e; i--)
#define all(v) v.begin(), v.end()
#define long long int int64
#define BIG(a, b) ( (a>b?a:b) )
int M[222222];
int res = -1;
int n, k;
int f(int idx)
{
if( idx != M[idx] )
{
M[idx] = f( M[idx] );
}
return M[idx];
}
void f2(int idx, int c)
{
if( idx != M[idx] )
{
f2( M[idx], c+1 );
}
else if(idx == k)
{
res = c;
}
}
int main()
{
//freopen("in.txt", "r+", stdin);
ios::sync_with_stdio(false);
in >> k >> n;
REPEQ(i, 1, k)
{
M[i] = i;
}
REP(i, n)
{
int x, y; in >> x >> y;
int px = f(x);
int py = f(y);
M[px] = M[py];
}
f2(1, 0);
out << res << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |