QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#333705#7997. 树 V 图guixinTL 0ms0kbC++142.5kb2024-02-20 13:00:142024-02-20 13:00:14

Judging History

你现在查看的是最新测评结果

  • [2024-02-20 13:00:14]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-02-20 13:00:14]
  • 提交

answer

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

#define K01 main

char buf[10000000],*p1,*p2;
#define getchar() ((p1==p2&&(p2=(p1=buf)+fread(buf,1,10000000,stdin),p1==p2))?0:*p1++)

void print(std::vector<int> x){
	for(int i = 0; i < x.size(); i++) printf("%d ", x[i]);
	putchar('\n');
}

inline long long read(){
	char ch = getchar();
	long long res = 0;
	short f = 1;
	for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f *= -1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
	return res * f;
}

class Solution{
	public:
		Solution(){}
		Solution(int __n, int __k): n(__n), k(__k), path(__n + 1), a(__k), f(__n + 1), flag(__n + 1){
			for(int i = 1; i < n; i++){
				int u = read(), v = read();
				path[u].push_back(v);
				path[v].push_back(u);
			}
			for(int i = 1; i <= n; i++) f[i] = read();
		}
		int solve(){
			make_vec(0);
			return ans;
		}
		void make_vec(int x){
			if(x == k) check();
			else{
				for(int i = 1; i <= n; i++) if(!flag[i]){
					a[x] = i;
					flag[i] = true;
					make_vec(x + 1);
					flag[i] = false;
				}
			}
		}
		void check(){
			for(int i = 1; i <= n; i++){
				std::vector<bool> vis(n + 1);
				std::queue<std::pair<int, int>> queue;
				queue.push({i, 0});
				vis[i] = true;
				while(!queue.empty()){
					std::pair<int, int> tmp = queue.front();
					int temp1 = tmp.first, temp4 = tmp.second;
					if(flag[temp1]) break;
					queue.pop();
					int temp3 = temp4 + 1;
					for(int j = 0; j < path[temp1].size(); j++){
						int temp2 = path[temp1][j];
						if(!vis[temp2]) queue.push({temp2, temp3}), vis[temp2] = true;;
					}
				}
				std::vector<bool> tmp(n + 1);
				std::pair<int, int> temp2 = queue.front();
				int temp1 = temp2.second;
				while(temp2.second == temp1){
					int temp3 = temp2.first;
					if(flag[temp3]) tmp[temp3] = true;
					queue.pop();
					if(queue.empty()) break;
					temp2 = queue.front();
				}
				for(int j = 0; j < k; j++)if(tmp[a[j]]){
					if(j + 1 != f[i]) return ;
					else break;
				}
			}
			ans++;
		}
		bool in_array(int x, std::vector<int> &vec){
			for(int i = 0; i < vec.size(); i++) if(x == vec[i]) return true;
			return false;
		}
	private:
		int n, k;
		int ans = 0;
		std::vector<int> a, f;
		std::vector<bool> flag;
		std::vector<std::vector<int>> path;
};

int K01(){
	int T = read();
	while(T--){
		int n = read(), k = read();
		Solution solution(n, k);
		printf("%d\n", solution.solve());
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

10
15 2
10 5
3 5
12 5
10 9
11 7
3 8
2 4
7 1
15 14
8 13
15 6
2 1
4 8
11 15
1 1 1 1 2 1 1 1 2 2 1 2 1 1 1
15 3
8 11
12 8
1 3
13 15
5 9
10 13
6 12
14 4
4 9
15 5
11 10
2 14
7 2
6 3
3 2 3 2 2 3 2 1 2 1 1 3 1 2 1
15 5
1 7
5 2
11 9
6 8
13 3
14 12
3 1
8 9
5 10
10 11
5 1
12 13
10 15
11 4
3 3 3 2 3 2 1 2 2 2 ...

output:


result: