Advertisement
This is comment for paste
HEHEHE
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct edge
- {
- int a;
- int b;
- };
- void sol(vector<string> id, vector<edge> edges)
- {
- int n = id.size(), m = edges.size(), MAX = 1;
- bool flag = true;
- vector<vector<int>> cost;
- cost.resize(n);
- for(int i=0;i<n;i++){
- cost[i].resize(n, INT_MAX);
- cost[i][i] = 0;
- }
- for(int i=0;i<m;i++){
- cost[edges[i].a][edges[i].b] = 1;
- cost[edges[i].b][edges[i].a] = 1;
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- for(int k=0;k<(int)cost.size();k++){
- if((cost[j][i]+cost[i][k])<cost[j][k]){
- cost[j][k] = cost[j][i] + cost[i][k];
- }
- }
- }
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- cout << cost[i][j] << " ";
- }
- }
- for(int i=0;i<(int)cost.size();i++){
- for(int j=0;j<(int)cost.size();j++){
- if(cost[i][j] == INT_MAX){
- flag = false;
- MAX = -1;
- break;
- }
- MAX = max(MAX, cost[i][j]);
- }
- if(!flag){
- break;
- }
- }
- cout << MAX << "\n";
- return;
- }
- int main()
- {
- int n, a, b, idx=0;
- while(cin >> n){
- vector<string> id;
- vector<edge> edges;
- string s1, s2;
- for(int i=0;i<n;i++){
- idx = 0;
- cin >> s1 >> s2;
- for(int j=0;j<(int)id.size();j++){
- if(s1==id[j]){
- idx += 10;
- a = j;
- }
- if(s2==id[j]){
- idx += 1;
- b = j;
- }
- }
- if(s1==s2 && idx==0){
- a = (int)id.size();
- id.push_back(s1);
- b = a;
- }
- else{
- if(idx==0){
- a = (int)id.size();
- id.push_back(s1);
- b = (int)id.size();
- id.push_back(s2);
- }
- else if(idx==1){
- a = (int)id.size();
- id.push_back(s1);
- }
- else if(idx>1){
- b = (int)id.size();
- id.push_back(s2);
- }
- if(a!=b){
- edges.push_back({a, b});
- }
- }
- }
- sol(id, edges);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement