Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("apm.in");
- ofstream fout("apm.out");
- const int nmax = 260;
- struct el
- {
- int nod, cost;
- }tt[nmax];
- struct muchie
- {
- int x, y, c;
- }mc[nmax*nmax];
- int n, m;
- vector <el> g[nmax];
- int cost[nmax];
- int mat[nmax][nmax];
- inline void read()
- {
- fin >> n;
- for (int i = 1; i <= n-1; i++)
- {
- int a, b, c;
- fin >> a >> b >> c;
- mat[a][b] = mat[b][a] = 1;
- g[a].push_back({b, c});
- g[b].push_back({a, c});
- }
- int x;
- while (fin >> x)
- {
- cost[++m] = x;
- }
- }
- inline void dfs(int nod,int stop, int tata)
- {
- if (nod == stop)
- return;
- for (auto it : g[nod])
- {
- int vecin = it.nod;
- int c = it.cost;
- if (vecin == tata)
- continue;
- tt[vecin].nod = nod;
- tt[vecin].cost = c;
- dfs(vecin, stop, nod);
- }
- }
- inline int caut(int i, int j)
- {
- int maxim = 0;
- while (i != j)
- maxim = max(maxim, tt[j].cost), j = tt[j].nod;
- return maxim+1;
- }
- int cnt;
- inline void getgraph()
- {
- for (int i = 1; i < n; i++)
- {
- for (int j = i+1; j <= n; j++)
- {
- //gasim maximul de pe ruta (i, j)
- if (mat[i][j])
- continue;
- dfs(i, j, -1);
- mc[++cnt] = {i, j, caut(i, j)};
- }
- }
- for (int i = 1; i <= cnt; i++)
- cout << mc[i].x << " " << mc[i].y << " " << mc[i].c << "\n";
- }
- int main()
- {
- read();
- getgraph();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement