QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188974#7502. Painting the Roadszhoukangyang#WA 4ms19852kbC++111.6kb2023-09-26 18:27:512023-09-26 18:27:51

Judging History

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

  • [2023-09-26 18:27:51]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:19852kb
  • [2023-09-26 18:27:51]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long 
#define vi vector < int > 
#define sz(a) ((int) (a).size())
#define ll long long 
#define ull unsigned long long
#define me(a, x) memset(a, x, sizeof(a)) 
using namespace std;
const int N = 1 << 19;
int n, m;
int col[N];

struct edge{
	int v, w, c;
};
int cnt[N];
vector < edge > e[N];

int sum[N];
void dfs2(int x, int fa) {
	sum[x] = cnt[x] - col[x]; 
	for(auto to : e[x]) if(to.v != fa) {
		int v = to.v;
		dfs2(v, x);
		sum[x] += sum[v];
	}
}

ll ans;
void dfs3(int x, int fa) { 
	for(auto to : e[x]) if(to.v != fa) {
		int v = to.v;
		int A = sum[1] - sum[v];
		int B = sum[v];
		if(A >= 0 && B >= 0) {
			ans += (A & 1) * to.w;
		} else {
			ans += (ll) min(abs(A), abs(B)) * to.w;
		}
		dfs3(v, x);
	}
}

void Main() {
	cin >> n >> m;
	L(i, 1, n - 1) {
		int u, v;
		edge nd;
		cin >> u >> v >> nd.w >> nd.c;
		col[u] ^= nd.c;
		col[v] ^= nd.c;
		nd.v = v, e[u].emplace_back(nd);
		nd.v = u, e[v].emplace_back(nd);
	}
	L(i, 1, m) {
		int p;
		cin >> p;
		col[p] ^= 1;
		++cnt[p];
	}
	int cols = 0;
	L(i, 1, n) {
		cols += col[i];
	}
	if(cols > m || ((m - cols) & 1)) {
		cout << "-1\n";
	} else {
		ans = 0;
		dfs2(1, 0);
		dfs3(1, 0);
		cout << ans << '\n';
	}
	
	L(i, 1, n) {
		e[i].clear();
		cnt[i] = 0;
		col[i] = 0;
		sum[i] = 0;
	}
}

int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin >> t; while(t--) Main();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19852kb

input:

5
3 2
1 2 1 1
2 3 2 1
1 3
4 2
1 2 3 1
2 3 1 0
3 4 4 1
1 2
5 4
1 2 3 0
2 3 1 1
3 4 2 0
4 5 2 1
1 1 1 1
5 2
1 2 2 1
1 3 3 0
1 5 2 1
3 4 1 1
1 2
10 5
1 2 10 1
2 3 3 1
3 4 4 0
4 5 4 1
5 6 2 1
2 7 8 0
2 8 9 1
4 9 1 0
1 10 4 0
10 10 2 1 8

output:

3
9
21
-1
42

result:

ok 5 number(s): "3 9 21 -1 42"

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 19836kb

input:

1000
5 5
1 2 4 1
2 3 9 0
3 4 10 1
3 5 8 1
1 5 2 5 1
5 3
1 2 7 1
1 3 7 0
2 4 9 0
3 5 4 1
3 4 3
5 3
1 2 7 1
2 3 1 0
1 4 7 1
4 5 5 1
4 4 3
5 1
1 2 3 1
1 3 6 0
2 4 10 0
2 5 7 0
1
5 3
1 2 10 1
1 3 10 0
1 4 1 1
3 5 4 0
2 5 2
5 5
1 2 7 0
1 3 5 0
2 4 8 1
2 5 10 0
2 2 3 5 4
5 4
1 2 6 1
1 3 4 0
3 4 4 0
1 5 5 ...

output:

22
-1
19
3
11
8
11
7
8
0
10
1
1
7
5
28
12
-1
19
16
12
13
-1
32
9
18
16
14
10
12
16
0
11
-1
17
-1
9
14
27
8
11
-1
6
6
15
18
46
0
14
9
-1
5
8
22
-1
-1
17
-1
25
6
0
24
6
15
21
15
22
-1
6
0
65
20
5
28
20
0
20
19
18
-1
10
0
16
9
19
6
21
11
11
4
6
20
11
0
8
8
31
8
23
-1
8
-1
11
-1
9
13
-1
-1
19
9
20
19
6
...

result:

wrong answer 600th numbers differ - expected: '19', found: '7'