Codeforces Round #567 (Div. 2)
D:
题意是在树中确定一个根,使得相同深度的点度数相同。
稍微画一下可以知道,根要么在直径上,要么在直径终点延伸出的一条链的端点上。
因此只需要处理直径的两个端点、直径的中点、直径延伸出的最短的链的端点这几个点就行。如果这几个点都不满足要求,则输出-1。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+7;
int deg[N], sz[N];
int rt;
vector<int> adj[N], pth;
int dl, dr, dd;
int dfsd(int u, int p, int &endp) {
int mx1=0, mx2=0;
int mxp1, mxp2;
for(int v : adj[u]) {
if(v==p) continue;
int pt;
int d = dfsd(v, u, pt)+1;
if(d>mx1) mx2=mx1, mxp2=mxp1, mx1=d, mxp1=pt;
else if(d>mx2) mx2=d, mxp2=pt;
}
if(mx1+mx2>dd) {
if(mx2) dl=mxp1, dr=mxp2;
else dl=mxp1, dr=u;
dd = mx1+mx2;
}
endp=mx1?mxp1:u;
return mx1;
}
bool dfs(int u, int p, int d) {
if(deg[d]==-1) deg[d] = adj[u].size();
else if(deg[d]!=adj[u].size()) return false;
for(int v : adj[u]) {
if(v==p) continue;
bool ok = dfs(v, u, d+1);
if(!ok) return false;
}
return true;
}
bool find_path(int u, int p, int e) {
if(u==e) {
pth.push_back(u);
return true;
}
for(int v : adj[u]) {
if(v==p) continue;
if(find_path(v, u, e)) {
pth.push_back(u);
return true;
}
}
return false;
}
int find_top(int u, int p, int d, int mxd) {
if(d>mxd) return -1;
if(adj[u].size()==1) return u;
if(p!=0&&adj[u].size()>=3) return -1;
for(int v : adj[u]) {
if(v==p) continue;
int res = find_top(v, u, d+1, mxd);
if(res!=-1) return res;
}
return -1;
}
int main() {
int n;
scanf("%d", &n);
for(int i=1; i<n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dl=1;dr=1;dd=0;
int tmp;
dfsd(1, 0, tmp);
// printf("%d %d %d\n", dl, dr, dd);
memset(deg, -1, sizeof(deg));
if(dfs(dl, 0, 0)) printf("%d\n", dl), exit(0);
memset(deg, -1, sizeof(deg));
if(dfs(dr, 0, 0)) printf("%d\n", dr), exit(0);
find_path(dl, 0, dr);
if(pth.size()%2==0) puts("-1"), exit(0);
int p = pth[pth.size()/2];
memset(deg, -1, sizeof(deg));
// printf("%d\n", p);
if(dfs(p, 0, 0)) printf("%d\n", p), exit(0);
// cout << p << endl;
int t = find_top(p, 0, 0, pth.size()/2-1);
// cout <<t << endl;
memset(deg, -1, sizeof(deg));
if(t!=-1&&dfs(t, 0, 0)) printf("%d\n", t), exit(0);
puts("-1");
return 0;
}
E:
虽然式子是连乘的,但是它们的指数是求和,因此将其指数用快速幂求出来即可。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9+7;
const ll p = 1e9+6;
struct Matrix {
ll mat[5][5];
int B;
Matrix(int _B) : B(_B) { memset(mat, 0, sizeof(mat)); }
ll* operator[] (int index) {
return mat[index];
}
Matrix operator * (Matrix a) {
Matrix c(B);
for(int i=0; i<B; ++i) {
for(int j=0; j<B; ++j) {
for(int k=0; k<B; ++k) {
c[i][j]=(c[i][j]+mat[i][k]*a[k][j]%p)%p;
}
}
}
return c;
}
};
Matrix powm(Matrix a, ll b) {
Matrix c(a.B);
for(int i=0; i<c.B; ++i) {
c[i][i]=1;
}
while(b) {
if(b&1) c=c*a;
a=a*a;
b>>=1;
}
return c;
}
ll powb(ll a, ll b ) {
ll c = 1;
while(b) {
if(b&1) c=c*a%mod;
a=a*a%mod;
b>>=1;
}
return c;
}
int main() {
ll n, f1, f2, f3, c;
cin >> n >> f1 >> f2 >> f3 >> c;
Matrix m(3);
m[0][0]=m[0][1]=m[0][2]=m[1][0]=m[2][1]=1;
m = powm(m, n-3);
ll fon = m[0][2];
ll ftw = m[0][1];
ll fth = m[0][0];
m = Matrix(5);
m[0][0]=m[0][1]=m[0][2]=m[0][3]=m[0][4]=1;
m[1][0]=m[2][1]=m[3][3]=m[3][4]=m[4][4]=1;
m = powm(m, n-3);
ll cf = m[0][4]*2;
// printf("%I64d %I64d %I64d %I64d\n", fon, ftw, fth, cf);
ll res = powb(f1, fon);
res = res * powb(f2, ftw)%mod;
res = res * powb(f3, fth)%mod;
res = res * powb(c, cf)%mod;
printf("%I64d\n", res);
}