Submission #441448

# Submission time Handle Problem Language Result Execution time Memory
441448 2021-07-05T08:14:30 Z daniel920712 Fountain Parks (IOI21_parks) C++17
0 / 100
3500 ms 204 KB
#include "parks.h"
#include <map>
#include <utility>
#include <queue>
#include <set>
using namespace std;
map < pair < int , int > , int > all,con;
map < pair < int , int > , pair < int , int > > road;
vector < int > u,v,a,b;
queue < pair < int , int > > BFS;
set < pair < int , int > > still;
int Father[200005];
int Find(int here)
{
    if(Father[here]==here) return here;
    Father[here]=Find(Father[here]);
    return Father[here];
}
int construct_roads(vector < int > x, vector < int > y)
{
    int N=x.size(),i,t,aa,bb,ok=1,xx,yy;
    for(i=0;i<N;i++)
    {
        all[make_pair(x[i],y[i])]=i;
        Father[i]=i;
    }
    for(i=0;i<N;i++)
    {
        if(all.find(make_pair(x[i],y[i]+2))!=all.end())
        {
            t=all[make_pair(x[i],y[i]+2)];
            road[make_pair(x[i],y[i]+1)]=make_pair(i,t);
            con[make_pair(x[i]+1,y[i]+1)]++;
            con[make_pair(x[i]-1,y[i]+1)]++;

            aa=Find(i);
            bb=Find(t);
            if(aa!=bb) Father[aa]=bb;

        }
        if(all.find(make_pair(x[i]+2,y[i]))!=all.end())
        {
            t=all[make_pair(x[i]+2,y[i])];
            road[make_pair(x[i]+1,y[i])]=make_pair(i,t);
            con[make_pair(x[i]+1,y[i]+1)]++;
            con[make_pair(x[i]+1,y[i]-1)]++;
            aa=Find(i);
            bb=Find(t);
            if(aa!=bb) Father[aa]=bb;
        }
    }
    t=Find(0);
    for(i=0;i<N;i++)  if(t!=Find(i)) ok=0;
    if(ok==0) return ok;
    for(auto i:con)
    {
        still.insert(i.first);
        if(i.second==1) BFS.push(i.first);
    }
    while(!BFS.empty())
    {
        xx=BFS.front().first;
        yy=BFS.front().second;
        BFS.pop();
        if(con[make_pair(xx,yy)]<=0) continue;
        if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy;
        if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy;
        if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1;
        if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1;
        u.push_back(road[make_pair(aa,bb)].first);
        v.push_back(road[make_pair(aa,bb)].second);
        a.push_back(xx);
        b.push_back(yy);
        road.erase(make_pair(aa,bb));
        con[make_pair(xx,yy)]=0;
        still.erase(make_pair(xx,yy));
        if(aa%2==0)
        {
            con[make_pair(aa-1,bb)]--;
            con[make_pair(aa+1,bb)]--;

            if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
            if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
        }
        else
        {
            con[make_pair(aa,bb-1)]--;
            con[make_pair(aa,bb+1)]--;

            if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
            if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
        }

    }
    while(!still.empty())
    {
        BFS.push(*still.begin());
        while(!BFS.empty())
        {
            ok=0;
            xx=BFS.front().first;
            yy=BFS.front().second;
            BFS.pop();
            if(con[make_pair(xx,yy)]<=0) continue;
            if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy,ok=1;
            if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy,ok=1;
            if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1,ok=1;
            if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1,ok=1;
            con[make_pair(xx,yy)]=0;
            still.erase(make_pair(xx,yy));
            if(ok==0) continue;
            u.push_back(road[make_pair(aa,bb)].first);
            v.push_back(road[make_pair(aa,bb)].second);
            a.push_back(xx);
            b.push_back(yy);
            road.erase(make_pair(aa,bb));

            if(aa%2==0)
            {
                con[make_pair(aa-1,bb)]--;
                con[make_pair(aa+1,bb)]--;

                if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
                if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
            }
            else
            {
                con[make_pair(aa,bb-1)]--;
                con[make_pair(aa,bb+1)]--;

                if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
                if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
            }

        }
    }
    if(!road.empty()) return 0;


    if(ok) build(u,v,a,b);
    return ok;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:88:32: warning: 'bb' may be used uninitialized in this function [-Wmaybe-uninitialized]
   88 |             con[make_pair(aa,bb+1)]--;
      |                              ~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Execution timed out 3559 ms 204 KB Time limit exceeded
3 Halted 0 ms 0 KB -