今天嫖来的两道题:
D
.
S
c
o
r
e
o
f
a
T
r
e
e
D. Score of a Tree
D.ScoreofaTree
E
.
E
d
g
e
R
e
v
e
r
s
e
E. Edge Reverse
E.EdgeReverse
D
D
D题是比较离谱的一道题,你在做的时候好像是dp,但是选择的情况太多了,其实对于每一个节点来说,除了叶节点之外,每一个节点的值为
0
0
0和
1
1
1的概率都是
0.5
0.5
0.5,那么最后异或和的概率还是
0.5
0.5
0.5(为
0
0
0和
1
1
1)这样的节点记为
A
A
A类节点,
A
A
A类节点迟早会被
B
B
B类节点所取代,
B
B
B类节点就是一定为
0
0
0的节点,也就是叶节点,那么一个
A
A
A节点对于总答案的贡献度就是
2
n
−
1
2^{n-1}
2n−1,而
B
B
B不贡献,所以要看这个节点能活多长时间就看他有多高,只要足够高,僵尸就不容易吃掉它的脑子。只要
B
B
B节点没上来,他就永远是
A
A
A类节点,其实看他有多高就是看他过了多少个
t
t
t时间。
参考:点我(写的很不错)
代码:
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int length = 2e5 + 5;
vector<vector<int>> edge(length);
int dp[length];
int vis[length];
int mod = 1e9 + 7;
void dfs(int cur)
{
vis[cur] = 1;
for (int v : edge[cur])
{
if (!vis[v])
{
vis[v] = 1;
dfs(v);
dp[cur]=max(dp[v] + 1, dp[cur]);
vis[v] = 0;
}
}
}
int ksm(int x, int n)
{
int sum = 1;
while (n)
{
if (n % 2 == 1)
{
sum = ((ll)sum*x) % mod;
}
n = n >> 1;
x = ((ll)x*x) % mod;
}
return sum;
}
int main(void)
{
int t;
scanf_s("%d", &t);
for (int i = 0; i < t; i++)
{
int n;
scanf_s("%d", &n);
for (int i = 1; i <= n; i++)
{
edge[i].clear();
dp[i] = 1;
}
for (int i = 0; i < n - 1; i++)
{
int a, b;
scanf_s("%d%d", &a, &b);
edge[a].push_back(b);
edge[b].push_back(a);
}
dfs(1);
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = (sum + dp[i] % mod) % mod;
}
sum = ((ll)sum*ksm(2, n - 1))%mod;
printf("%d\n", sum);
}
}
E
E
E题是最小化最大值,可以二分枚举最大值,还是参考刚才的文章。这里面主要是这个拓扑排序比较闹心,这个图里面可能有自环,所以必须得缩点,为啥非得缩点?我一开始的时候没有缩点做的,但是怎么都不对,如果有两个不连通的环,那么没法把入度为
0
0
0的点放进去,后来又想,既然找不到就随便放进去一个点(反正有环),但是可能是一个连通分量含有一个环,如果随便放一个点,可能不是起始的,(那个环可能没有点指向
代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#define PII pair<int,int>
using namespace std;
const int length = 2e5 + 5;
vector<vector<PII>> edge(length);
vector<vector<int>> tmp;
int color[length];
int dfn[length];
int low[length];
int in_stack[length];
int cl = 0;
int cnt = 0;
stack<int> s;
void tarjan(int cur,int fa)
{
dfn[cur] = ++cnt;
low[cur] = dfn[cur];
in_stack[cur] = 1;
s.push(cur);
for (int v : tmp[cur])
{
if (!dfn[v])
{
tarjan(v, cur);
low[cur] = min(low[cur], low[v]);
}
else if (in_stack[v])
low[cur] = min(low[cur], dfn[v]);
}
if (low[cur] == dfn[cur])
{
cl++;
while (s.top() != cur)
{
int tmp = s.top();
color[tmp] = cl;
in_stack[tmp] = 0;
s.pop();
}
color[cur] = cl;
in_stack[cur] = 0;
s.pop();
}
}
bool check(int x,int n)
{
vector<vector<int>> edge1(n + 5);
vector<int> rudu(n + 5);
for (int i = 1; i <= n; i++)
{
color[i] = 0;
dfn[i] = 0;
low[i] = 0;
for (auto tmp : edge[i])
{
int c = tmp.second;
int a = i;
int b = tmp.first;
if (c <= x)
{
edge1[a].push_back(b);
edge1[b].push_back(a);
// rudu[a]++;
}
else
{
//edge1[b].push_back(a);
edge1[a].push_back(b);
}
}
}
cnt = 0;
cl = 0;
tmp = edge1;
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i,-1);
vector<vector<int>> edge2(n + 5);
for (int i = 1; i <= n; i++)
{
for (int v : edge1[i])
{
if (color[i] != color[v])
{
edge2[color[i]].push_back(color[v]);
rudu[color[v]]++;
}
}
}
queue<int> q;
for (int i = 1; i <= cl; i++)
{
if (rudu[i] == 0)
q.push(i);
}
if (q.size() > 1)
return 0;
vector<int> vis(n + 5, 0);
while (!q.empty())
{
int tmp = q.front();
vis[tmp] = 1;
q.pop();
for (int v : edge2[tmp])
{
if (!vis[v])
q.push(v);
vis[v] = 1;
}
}
for (int i = 1; i <= cl; i++)
if (!vis[i])
return 0;
return 1;
}
int main(void)
{
int t;
scanf_s("%d", &t);
for (int i = 0; i < t; i++)
{
int n, m;
scanf_s("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
edge[i].clear();
}
for (int i = 0; i < m; i++)
{
int a, b, c;
scanf_s("%d%d%d", &a, &b, &c);
edge[a].push_back({ b,c });
}
int l = -1;
int r = INT_MAX;
while (l < r-1)
{
int mid = ((long long)l + r) / 2;
if (!check(mid,n))
{
l = mid;
}
else
r = mid;
}
if (r == INT_MAX)
r = -1;
printf("%d\n", r);
}
}