Submission #229952


Source Code Expand

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define CLR(a) memset((a), 0 ,sizeof(a))
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define SZ(a) int((a).size())
#define PB push_back
#define MP make_pair

#define MAX_V 100001
#define INF 100000

struct edge{int to,cost;};
typedef pair<int,int> P;//firstは最短距離,secondは頂点番号

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));
	
	while(!que.empty()){
		P p=que.top();que.pop();//仮の最短距離が短い順に取り出す
		int v=p.second;
		if(d[v]<p.first)continue;
		REP(i,G[v].size()){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));//仮の最短距離と頂点の組を更新が行われる度に追加していく
			}
		}
	}
}
int main(){
	int N;
	cin>>N;
	V=N;
	REP(i,N-1){
		int x,y;
		cin>>x>>y;
		x--;y--;
		edge e1,e2;
		e1.to=y;e1.cost=1;
		e2.to=x;e2.cost=1;
		G[x].PB(e1);
		G[y].PB(e2);
	}
	int Q;
	cin>>Q;
	vector<int>res;
	REP(i,Q){
		int a,b;
		cin>>a>>b;
		a--;b--;
		dijkstra(a);
		res.PB(d[b]+1);
	}
	REP(i,Q){
		cout<<res[i]<<endl;
	}
	return 0;
}

Submission Info

Submission Time
Task D - 閉路
User blue0620
Language C++ (G++ 4.6.4)
Score 30
Code Size 1700 Byte
Status TLE
Exec Time 2037 ms
Memory 7576 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 0 / 70
Status
AC × 3
AC × 15
AC × 17
TLE × 10
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask1 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
Subtask2 subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 26 ms 3232 KB
subtask0_sample02.txt AC 26 ms 3224 KB
subtask0_sample03.txt AC 26 ms 3104 KB
subtask1_01.txt AC 136 ms 6568 KB
subtask1_02.txt AC 139 ms 6584 KB
subtask1_03.txt AC 27 ms 3232 KB
subtask1_04.txt AC 28 ms 3028 KB
subtask1_05.txt AC 27 ms 3108 KB
subtask1_06.txt AC 26 ms 3108 KB
subtask1_07.txt AC 206 ms 7460 KB
subtask1_08.txt AC 190 ms 7336 KB
subtask1_09.txt AC 179 ms 7208 KB
subtask1_10.txt AC 218 ms 7072 KB
subtask1_11.txt AC 205 ms 7068 KB
subtask1_12.txt AC 202 ms 7072 KB
subtask2_01.txt TLE 2037 ms 6688 KB
subtask2_02.txt TLE 2031 ms 6696 KB
subtask2_03.txt AC 343 ms 3744 KB
subtask2_04.txt AC 1301 ms 3748 KB
subtask2_05.txt TLE 2032 ms 3492 KB
subtask2_06.txt TLE 2030 ms 3492 KB
subtask2_07.txt TLE 2032 ms 7576 KB
subtask2_08.txt TLE 2031 ms 7396 KB
subtask2_09.txt TLE 2031 ms 7280 KB
subtask2_10.txt TLE 2032 ms 7216 KB
subtask2_11.txt TLE 2032 ms 7212 KB
subtask2_12.txt TLE 2032 ms 7212 KB