이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int inf = 2e9;
int n;
vector<pair<int,pair<int,int>>> e;
vector<int> g[25];
bool iau[25];
int ans;
int fr[15];
vector<int> stk;
vector<int> ret;
int Y;
void dfs(int nod, int tata)
{
stk.push_back(nod);
if (nod == Y)
ret = stk;
for (auto vecin : g[nod])
if (vecin != tata)
dfs(vecin,nod);
stk.pop_back();
}
vector<int> get_path(int x,int y)
{
Y = y;
stk.clear();
dfs(x,0);
return ret;
}
void afis()
{
for (int i = 1; i <= n; i++)
fr[i] = 0;
int cst = 0;
for (int i = 0; i < n - 1; i++)
{
if (!iau[i])
continue;
if (iau[n - 1 + i])
fr[e[i].second.first]++;
else
fr[e[i].second.second]++;
cst += e[i].first;
}
for (int i = 1; i <= n; i++)
if (fr[i] != 0 and fr[i] != 2)
return;
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
if (fr[i] == 2 and fr[j] == 2)
{
vector<int> noduri = get_path(i,j);
bool ok = false;
for (int q = 0; q < e.size(); q++)
{
pair<int,int> edg = e[q].second;
if (!iau[q])
continue;
if (edg.first == i and edg.second == noduri[1])
ok = true;
if (edg.first == noduri[1] and edg.second == i)
ok = true;
if (edg.first == j and edg.second == noduri[noduri.size() - 2])
ok = true;
if (edg.first == noduri[noduri.size() - 2] and edg.second == j)
ok = true;
}
if (!ok)
return;
}
}
}
/*if (cst == 4)
{
for (int i = 0; i < 2 * n - 2; i++)
{
if (iau[i])
cout << 1;
else
cout << 0;
}
cout << endl;
}*/
ans = max(ans,cst);
}
/*
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
void bkt(int pos)
{
if (pos == 2 * n - 2)
afis();
else
{
iau[pos] = false;
bkt(pos + 1);
iau[pos] = true;
bkt(pos + 1);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i < n; i++)
{
int x,y,z;
cin >> x >> y >> z;
e.push_back({z,{x,y}});
g[x].push_back(y);
g[y].push_back(x);
}
if (n <= 2)
{
cout << 0;
return 0;
}
bkt(0);
cout << ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void afis()':
beads.cpp:64:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int q = 0; q < e.size(); q++)
| ~~^~~~~~~~~~
# | 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... |