Submission #1011946

#TimeUsernameProblemLanguageResultExecution timeMemory
1011946andrei_iorgulescuBeads and wires (APIO14_beads)C++14
13 / 100
8 ms456 KiB
#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] and iau[n - 1 + q])
                        ok = true;
                    if (edg.first == noduri[1] and edg.second == i and !iau[n - 1 + q])
                        ok = true;
                    if (edg.first == j and edg.second == noduri[noduri.size() - 2] and iau[n - 1 + q])
                        ok = true;
                    if (edg.first == noduri[noduri.size() - 2] and edg.second == j and !iau[n - 1 + q])
                        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;
}

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...