Submission #256579

# Submission time Handle Problem Language Result Execution time Memory
256579 2020-08-02T21:41:03 Z PedroBigMan Friend (IOI14_friend) C++14
27 / 100
38 ms 3932 KB
#include "friend.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000LL
#define EPS 0.00000001
#define pi 3.14159
ll mod=99824LL;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

ll f(ll bin,vector<vector<ll> > adj, ll N,vector<ll> c)
{
    vector<bool> in; 
    REP(i,0,N) {in.pb((bin&(1LL<<i))>0);}
    REP(i,0,N)
    {
        REP(j,i+1,N)
        {
            if(in[i]&&in[j]&&(find(whole(adj[i]),j)!=adj[i].end())) {return 0;}
        }
    }
    ll ans=0LL; REP(i,0,N) {if(in[i]) {ans+=c[i];}}
    return ans;
}

class Tree
{
    public:
    ll N; 
    vector<ll> p; 
    vector<vector<ll> > sons;
    vector<vector<ll> > down;
    vector<vector<ll> > adj;
    ll root;
    vector<bool> visited;
    vector<ll> val;
    vector<pl> MIS; //MIS[i].ff=MIS on the i-subtree with i, MIS[i].ss=MIS on the i-subtree without i
    
    Tree(vector<vector<ll> > ad, ll r=0LL)
    {
        N=ad.size(); root=r; adj=ad;
        REP(i,0,N) {visited.pb(false);}
        vector<ll> xx; REP(i,0,N) {sons.pb(xx); p.pb(-1);}
        DFS_Build(r,r);
        REP(i,0,N) {MIS.pb(mp(0LL,0LL));}
    }
    
    void Reset()
    {
        REP(i,0,N) {visited[i]=false;}
    }
    
    void DFS_Build(ll s, ll par)
    {
        p[s]=par;
        visited[s]=true;
        REP(i,0,adj[s].size())
        {
            if(adj[s][i]==par) {continue;}
            sons[s].pb(adj[s][i]);
            DFS_Build(adj[s][i],s);
        }
        return;
    }
    
    void Extend_BFS_Tree()
    {
        REP(i,0,N) {REP(j,0,down[i].size()) {sons[i].pb(down[i][j]);}}
    }
    
    void DFS(ll s)
    {
        if(visited[s]) {return;}
        REP(i,0,sons[s].size())
        {
            DFS(sons[s][i]);
        }
        MIS[s].ff=val[s]; REP(i,0,sons[s].size()) {MIS[s].ff+=MIS[sons[s][i]].ss;}
        MIS[s].ss=0LL; REP(i,0,sons[s].size()) {MIS[s].ss+=max(MIS[sons[s][i]].ff,MIS[sons[s][i]].ss);}
        visited[s]=true;
    }
    
    
};

class Graph
{
    public:
    ll N;
    vector<vector<ll> > adj; 
    vector<ll> visited; //for DFS/BFS
    vector<ll> current; //for CC
    vector<ll> og;
    
    Graph() {ll N=0LL;}
    
    Graph(vector<vector<ll> > ad)
    {
        adj=ad; N=adj.size(); REP(i,0,N) {visited.pb(false);}
    }
    
    void Reset()
    {
        REP(i,0,N) {visited[i]=false;}
        current.clear();
    }
    
    void DFS(ll s) 
    {
        if(visited[s]) {return;}
        visited[s]=true;
        current.pb(s); //only needed for CC
        REP(i,0,adj[s].size())
        {
            if(!visited[adj[s][i]]) {DFS(adj[s][i]);}
        }
        return;
    }
    
    vector<ll> BFS(ll s)
    {
        Reset();
        vector<ll> distance; REP(i,0,N) {distance.pb(INF);}
        REP(i,0,N) {visited[i]=false;}
        distance[s]=0; visited[s]=true;
        deque<ll> d; d.pb(s); ll cur;
        while(!d.empty())
        {
            cur=d.front(); d.pop_front();
            REP(i,0,adj[cur].size())
            {
                if(!visited[adj[cur][i]]) 
                {
                    visited[adj[cur][i]]=true; 
                    d.pb(adj[cur][i]); 
                    distance[adj[cur][i]]=distance[cur]+1;
                }
            }
        }
        return distance;
    }
    
    Tree BFS_Tree(ll s)
    {
        vector<ll> xx; vector<vector<ll> > ad,down; REP(i,0,N) {ad.pb(xx); down.pb(xx);}
        vector<ll> d = BFS(s);
        Reset();
        visited[s]=true;
        REP(i,0,N)
        {
            REP(j,0,adj[i].size())
            {
                ll c = adj[i][j];
                if(d[c]==d[i]+1 && !visited[c]) {visited[c]=true; ad[i].pb(c); ad[c].pb(i);}
                else if(d[c]>=d[i]) {down[i].pb(c);}
            }
        }
        Tree ANS(ad,s); ANS.down=down;
        return ANS;
    }

    vector<vector<ll> > CC()
    {
        Reset();
        ll fi=0; ll missing=N; REP(i,0,N) {visited[i]=false;}
        vector<vector<ll> > ans;
        while(missing>0)
        {
            REP(i,fi,N) {if(!visited[i]) {fi=i; break;}}
            current.clear();
            DFS(fi);
            ans.pb(current);
            missing-=current.size();
        }
        return ans;
    }
    
    vector<Graph> CCG()
    {
        Reset();
        vector<Graph> ans;
        vector<vector<ll> > CC=(*this).CC();
        unordered_map<ll,ll> m;vector<ll> xx; vector<vector<ll> > ad;
        vector<ll> ma;
        REP(cc,0,CC.size()) 
        {
            m.clear(); ma.clear();
            ad.clear(); 
            ll NN=CC[cc].size();
            REP(i,0,NN) {ad.pb(xx);}
            REP(i,0,NN) {m[CC[cc][i]]=i; ma[i]=ma[CC[cc][i]];}
            ll a,b;
            REP(i,0,NN)
            {
                a=CC[cc][i];
                REP(j,0,adj[a].size()) {b=adj[a][j]; ad[i].pb(m[b]);}
            }
            Graph H(ad); H.og=ma;
            ans.pb(H);
        }
        return ans;
    }
};

// Find out best sample
int findSample(int n,int confidence[],int host[],int protocol[])
{
    ll N = (ll) n; vector<ll> c,h,p; REP(i,0,N) {c.pb((ll) confidence[i]); h.pb((ll) host[i]); p.pb((ll) protocol[i]);}
    if(N<=10LL)
    {
        vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);}
        REP(i,1,N)
        {
            if(p[i]==0) {adj[i].pb(h[i]); adj[h[i]].pb(i);}
            else if(p[i]==1) 
            {
                REP(j,0,adj[h[i]].size()) {ll nei = adj[h[i]][j]; adj[i].pb(nei); adj[nei].pb(i);}
            }
            else
            {
                REP(j,0,adj[h[i]].size()) {ll nei = adj[h[i]][j]; adj[i].pb(nei); adj[nei].pb(i);}
                adj[i].pb(h[i]); adj[h[i]].pb(i);
            }
        }
        ll ans=0LL;
        REP(i,0,(1LL<<N)) {ans=max(ans,f(i,adj,N,c));}  
        return ans;
    }
    else if(p[1]==1)
    {
        ll sum=0LL; REP(i,0,N) {sum+=c[i];}
        return sum;
    } 
    else if(p[1]==2) 
    {
        return (*max_element(whole(c))); 
    }
    else if(p[1]==0)
    {
        vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);}
        REP(i,1,N) {adj[i].pb(h[i]); adj[h[i]].pb(i);}   
        Tree T(adj); T.val = c; T.DFS(0);
        return max(T.MIS[0].ff,T.MIS[0].ss);
    }
    else
    {
        vector<ll> xx; vector<vector<ll> > adj; REP(i,0,N) {adj.pb(xx);}
        unordered_set<ll> root; root.insert(0);
        REP(i,1,N)
        {
            if(p[i]==0) {adj[i].pb(h[i]); adj[h[i]].pb(i);}
            else if(p[i]==1) 
            {
                REP(j,0,adj[h[i]].size()) {ll nei = adj[h[i]][j]; adj[i].pb(nei); adj[nei].pb(i);}
            }
            else
            {
                REP(j,0,adj[h[i]].size()) {ll nei = adj[h[i]][j]; adj[i].pb(nei); adj[nei].pb(i);}
                adj[i].pb(h[i]); adj[h[i]].pb(i);
            }
            if(adj[i].size()==0) {root.insert(i);}
        }
        Graph G(adj); vector<Graph> CCG=G.CCG(); 
        ll ans=0LL;
        REP(i,0,CCG.size())
        {
            ll r;
            REP(j,0,CCG[i].N) {if(root.find(CCG[i].og[j])!=root.end()) {r=j; break;}}
            Tree T = CCG[i].BFS_Tree(r); T.Extend_BFS_Tree();
            T.DFS(T.root);
            ans+=max(T.MIS[T.root].ff,T.MIS[T.root].ss);
        }
        return ans;
    }
    return 0LL;
}

Compilation message

friend.cpp: In constructor 'Graph::Graph()':
friend.cpp:122:17: warning: unused variable 'N' [-Wunused-variable]
     Graph() {ll N=0LL;}
                 ^
friend.cpp: In function 'int findSample(int, int*, int*, int*)':
friend.cpp:296:39: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
             Tree T = CCG[i].BFS_Tree(r); T.Extend_BFS_Tree();
                                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Incorrect 1 ms 640 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Incorrect 1 ms 640 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Incorrect 38 ms 3932 KB Output isn't correct
13 Halted 0 ms 0 KB -