交换瓶子 (环、置换群)
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dat[10010];
bool st[10010];
int main(void)
{
int n;
cin >> n;
//注意从1开始读取,否则需要下标减一对应,造成麻烦
for(int i = 1; i <= n; i++)
cin >> dat[i];
int cnt = 0;
for(int i = 1; i <= n;i++)
if(!st[i])
{
cnt++;
int j = dat[i];
while(!st[j])
{
st[j] = true;
j = dat[j];
}
}
cout << n - cnt << endl;
return 0;
}
大臣的旅费
代码 -使用vector存储邻接表:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
struct Node{
int x,w;
};
vector <Node> h[N];
int dis[N];
int n;
void dfs(int u, int father , int distance)
{
dis[u] = distance;
for(int i = 0; i < h[u].size(); i++)
if(h[u][i].x != father)
dfs(h[u][i].x , u, distance + h[u][i].w);
/**
for(auto node : a[u])
if(node.x != father)
dfs(node.x , u , distance + node.w);
*/
}
int main(void)
{
cin >> n;
int x1 ,x2 ,d;
for(int i = 0; i < n - 1; i++)
{
scanf("%d%d%d" ,&x1, &x2, &d);
h[x1].push_back({x2 , d});
h[x2].push_back({x1 , d});
}
//城市从1开始编号
dfs(1 , -1 , 0);
int u = 1;
for(int i = 1; i <= n; i++)
if(dis[i] > dis[u])
u = i;
//从直径的起点开始找终点
dfs(u , -1 , 0);
for(int i = 1; i <= n; i++)
if(dis[i] > dis[u])
u = i;
int s = dis[u];
printf("%lld" , 10 * s + (1ll + s) * s / 2);
return 0;
}