题目一:
代码一:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n,m,a;
cin>>n>>m>>a;
string s[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>s[i][j];
}
}
int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int k=0;k<a;k++)
{
if(s[i][j][k]!=s[(i+1)%n][(j+1)%m][k])
{
count++;
}
}
}
}
cout<<count<<endl;
return 0;
}
题目二:
解题思路:
从 x, y 这两个选定边的端点出发,分别深搜找一条最长的路径,然后将两条路径合起来计算总长度。
代码二:
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int dfs(vector<vector<int>> &p, map<int, bool> &visited, int x, int depth){
int max = depth;
for(int i = 0; i < p[x].size(); i++){
if(!visited[p[x][i]]){
visited[p[x][i]] = true;
int tmp = dfs(p, visited, p[x][i], depth + 1);
if(tmp > max){
max = tmp;
}
visited[p[x][i]] = false;
}
}
return max;
}
int main(){
int n, x, y;
cin >> n;
vector<vector<int>> p(n+1);
for(int i = 1; i < n; i++){
int tmp;
cin >> tmp;
p[tmp].push_back(i+1);
p[i+1].push_back(tmp);
}
cin >> x >> y;
map<int, bool> visited;
for(int i = 1; i <= n; i++){
visited[i] = false;
}
visited[x] = true;
visited[y] = true;
int maxx = -1, maxy = -1;
maxx = dfs(p, visited, x, 0);
maxy = dfs(p, visited, y, 0);
cout << maxx + maxy + 1 << endl;
return 0;
}