This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
#define pb push_back
#define popb pop_back
#define all(v) (v).begin(),(v).end()
#define ff first
#define ss second
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX_N = 4005;
int n;
ll adj[MAX_N][MAX_N];
bool visited[MAX_N];
ll dfs(int u)
{
visited[u] = true;
ll maxt = 0;
for(int i = 1; i <= n; i ++)
{
if(visited[i])
continue;
maxt = max(maxt,dfs(i)+adj[u][i]);
}
visited[u] = false;
return maxt;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
memset(adj,0,sizeof(adj));
cin >> n;
for(int i = 1; i <=n; i ++)
{
ll u, dist;
cin >> u >> dist;
adj[i][u] = adj[u][i] = max(adj[i][u],dist);
}
ll ans = 0;
for(int i = 1; i <=n; i ++)
{
ans=max(ans,dfs(i));
}
cout << ans;
}
/*
Sample test case:
7
3 8
7 2
4 2
1 4
1 9
3 4
2 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |