답안 #601539

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
601539 2022-07-22T07:38:22 Z 장태환(#8473) Stranded Far From Home (BOI22_island) C++17
0 / 100
1000 ms 32968 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>link[200100];
vector<pair<int,int>>x;
int uf[200100];
int sum[200100];
int mi[200100];
int cnt[200100];
int f(int n)
{
    if(uf[n]==n)
    return n;
    return uf[n]=f(uf[n]);
}
void u(int n,int m)
{
    n=f(n);
    m=f(m);
    if(n==m)
        return;
    sum[n]+=sum[m];
    if(mi[n]==mi[m])
        cnt[n]+=cnt[m];
    else if(mi[n]>mi[m])
    {
        mi[n]=mi[m];
        cnt[n]=cnt[m];
    }
    uf[m]=n;
}
vector<int>carry;
int arr[200100];
int ans[200100];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N,M;
    cin >>N>>M;
    int i;
    for(i=0;i<N;i++)
        cin >>arr[i+1];
    for(i=0;i<M;i++)
    {
        int a,b;
        cin >>a>>b;
        link[a].push_back(b);
        link[b].push_back(a);
        x.push_back({a,b});
    }
    int summ=0;
    vector<pair<int,int>>t;
    for(i=0;i<N;i++)
    {
        summ+=arr[i+1];
        t.push_back({arr[i+1],i+1});
        carry.push_back(i+1);
    }
    sort(t.begin(),t.end());
    for(i=0;i<32;i++)
    {
        int ve=(1LL<<i);
        int i;
        memset(uf,-1,sizeof(uf));
        memset(sum,0,sizeof(sum));
        memset(mi,10,sizeof(mi));
        memset(cnt,0,sizeof(cnt));
        for(i=0;i<N;i++)
        {
            if(t[i].first<ve)
            {
                uf[t[i].second]=t[i].second;
                sum[t[i].second]=t[i].first;
                int j;
                for(j=0;j<link[t[i].second].size();j++)
                {
                    if(uf[link[t[i].second][j]]>=0)
                    {
                        u(link[t[i].second][j],t[i].second);
                    }
                }
            }
            else
            {
                unordered_map<int,int>maaa;
                int j;
                for(j=0;j<link[t[i].second].size();j++)
                {
                    if(uf[link[t[i].second][j]]>=0)
                    {
                        if(maaa[f(link[t[i].second][j])])
                            continue;
                        maaa[f(link[t[i].second][j])]=1;
                        if(mi[f(link[t[i].second][j])]==t[i].first)
                            cnt[f(link[t[i].second][j])]++;
                        else if(mi[f(link[t[i].second][j])]>t[i].first)
                        {
                            mi[f(link[t[i].second][j])]=t[i].first;
                            cnt[f(link[t[i].second][j])]=1;
                        }
                    }
                }
            }
        }
        vector<int>nc;
        for(i=0;i<carry.size();i++)
        {
            if(arr[carry[i]]<ve)
            {
                int v=sum[f(carry[i])];
                if(mi[f(carry[i])]<=v)
                    v+=mi[f(carry[i])];
                if(v>=summ)
                    ans[carry[i]]=1;
                else if(v>=2*ve)
                {
                    nc.push_back(carry[i]);
                }
            }
            else
            {
                int v=arr[carry[i]];
                int ma=1LL<<60;
                int j;
                vector<int>arrr;
                for(j=0;j<link[carry[i]].size();j++)
                {
                    if(uf[link[carry[i]][j]]>=0)
                        arrr.push_back(f(link[carry[i]][j]));
                    else
                        arrr.push_back(link[carry[i]][j]);
                }
                sort(arrr.begin(),arrr.end());
                arrr.erase(unique(arrr.begin(),arrr.end()),arrr.end());
                for(j=0;j<arrr.size();j++)
                {
                    if(uf[arrr[j]]>=0)
                    {
                        if(mi[arrr[j]]!=arr[carry[i]]||cnt[arrr[j]]!=1)
                            ma=min(ma,mi[arrr[j]]);
                        v+=sum[arrr[j]];
                    }
                    else
                    {
                        ma=min(ma,arr[arrr[j]]);
                    }
                }
                if(ma<=v)
                    v+=ma;
                if(v>=summ)
                    ans[carry[i]]=1;
                else if(v>=2*ve)
                {
                    nc.push_back(carry[i]);
                }
            }
        }
        carry=nc;
    }
    for(i=1;i<=N;i++)
        cout <<ans[i];
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:76:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |                 for(j=0;j<link[t[i].second].size();j++)
      |                         ~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:88:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |                 for(j=0;j<link[t[i].second].size();j++)
      |                         ~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:107:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |         for(i=0;i<carry.size();i++)
      |                 ~^~~~~~~~~~~~~
island.cpp:127:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |                 for(j=0;j<link[carry[i]].size();j++)
      |                         ~^~~~~~~~~~~~~~~~~~~~~~
island.cpp:136:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |                 for(j=0;j<arrr.size();j++)
      |                         ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11220 KB Output is correct
2 Correct 12 ms 11220 KB Output is correct
3 Correct 12 ms 11220 KB Output is correct
4 Incorrect 21 ms 11496 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 11300 KB Output is correct
2 Correct 15 ms 11220 KB Output is correct
3 Execution timed out 1086 ms 32968 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11296 KB Output is correct
2 Execution timed out 1091 ms 31944 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 11220 KB Output is correct
2 Execution timed out 1094 ms 32052 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 11220 KB Output is correct
2 Correct 12 ms 11220 KB Output is correct
3 Correct 12 ms 11220 KB Output is correct
4 Incorrect 21 ms 11496 KB Output isn't correct
5 Halted 0 ms 0 KB -