제출 #1120177

#제출 시각아이디문제언어결과실행 시간메모리
1120177modwwe물병 (JOI14_bottle)C++17
100 / 100
1274 ms236172 KiB
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC optimize("conserve-stack")
#include<bits/stdc++.h>
#define int  long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define mp make_pair
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
#define getchar_unlocked getchar

inline int scan()
{
    char c = getchar_unlocked();
    int x = 0;
    while (c < '0' || c > '9')
    {
        c = getchar_unlocked();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + c - '0';
        c = getchar_unlocked();
    }
    return x;
}
void phongbeo();
const int inf = 1e9;
const ll mod2 = 1e9 + 7;
const int  mod1 = 998244353;
const ll base=67;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int add(int x,int y)
{
    if(x+y>=mod2) x-=mod2;
    if(x+y<0)x+=mod2;
    return x+y;
}
struct icd
{
    long double a;
    int b;
};
struct ib
{
    int a;
    int b;
};
struct ic
{
    int a, b, c;
};
struct id
{
    int a, b, c, d;
};
struct ie
{
    int a, b, c, d, e;

};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int  i, s10, s12,k1,k2,k3,s11,t,lim,w,l,r ;
int kk;
int el = 19;
main()
{
    if(fopen(task".inp","r"))
    {
        fin(task);
        fou(task);
    }
    NHP
    /// cin>>s1;
    // modwwe
    phongbeo();
    // checktime
}
bool cmp(ic a,ic b)
{
    return a.c<b.c;
}
int query;
int dist[2001][2001];
int b[2001][2001];
bool a[2001][2001];
int st[18][200001];
int cost[18][200001];
int in[200001];
int ou[200001];
vector<ic> v;
vector<ib> ke[200001];
deque<id> p;
struct ds
{
    ib dsu[200001];
    void reset(int x)
    {
        dsu[x]= {1,x};
    }
    int get(int x)
    {
        if(dsu[x].b==x) return x;
        return dsu[x].b=get(dsu[x].b);
    }
    bool noi(int x,int y)
    {
        x=get(x);
        y=get(y);
        if(x==y) return 0;
        if(dsu[x].a<dsu[y].a) swap(x,y);
        dsu[x].a+=dsu[y].a;
        dsu[y].b=x;
        return 1;
    }
} ds;
void dfs(int x,int y)
{
    st[0][x]=y;
    in[x]=++dem;
    for(auto f:ke[x])
        if(f.a^y)
        {
            dfs(f.a,x);
            cost[0][f.a]=f.b;
        }
    ou[x]=dem;
}
bool check(int x,int y)
{
    if(in[x]<=in[y]&&in[y]<=ou[x]) return 1;
    return 0;
}
int lca(int x,int y)
{
    int s=0;
    if(check(x,y)) return 0;
    for(int i=17; i>=0; --i)
        if(!check(st[i][x],y))
        {
            s=max(s,cost[i][x]);
            x=st[i][x];
        }
    s=max(s,cost[0][x]);
    return s;
}
int solve(int x,int y)
{
    if(ds.get(x)!=ds.get(y)) return -1;
    return max(lca(x,y),lca(y,x));
}
void phongbeo()
{
    cin>>n>>m>>k>>query;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            char x;
            cin>>x;
            if(x=='.') a[i][j]=1;
            else a[i][j]=0;
        }
    for(int i=1; i<=k; i++)
    {
        ds.reset(i);
        cin>>l>>r;
        p.push_front({0,l,r,i});
    }
    while(!p.empty())
    {
        id  x=p.front();
        p.pop_front();
        if(b[x.b][x.c]!=0) continue;
        b[x.b][x.c]=x.d;
        dist[x.b][x.c]=x.a;
        for(int j=0; j<4; j++)
        {
            l2=x.b+dx[j];
            r2=x.c+dy[j];
            if(l2<=n&&l2>0&&r2<=m&&r2>0&&a[l2][r2]&&b[l2][r2]==0)
                p.pb({x.a+1,l2,r2,x.d});
        }
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            for(int f=0; f<4; f++)
            {
                l2=i+dx[f];
                r2=j+dy[f];
                if(l2<=n&&l2>0&&r2<=m&&r2>0&&b[l2][r2]!=b[i][j]&&min(b[i][j],b[l2][r2])!=0)
                    v.pb({b[l2][r2],b[i][j],dist[i][j]+dist[l2][r2]});
            }
    sort(v.begin(),v.end(),cmp);
    for(auto x:v)
        if(ds.noi(x.a,x.b))
        {
            ke[x.a].pb({x.b,x.c});
            ke[x.b].pb({x.a,x.c});
        }
    for(int i=1; i<=k; i++)
        if(!in[i])
        {
            dfs(i,0);
        }
    ou[0]=dem;
    for(int i=1; i<18; i++)
        for(int j=1; j<=k; j++)
        {
            st[i][j]=st[i-1][st[i-1][j]],
                     cost[i][j]=max(cost[i-1][j],cost[i-1][st[i-1][j]]);
        }
    while(query--)
    {
        cin>>l>>r;
        cout<<solve(l,r),down
    }
}

컴파일 시 표준 에러 (stderr) 메시지

bottle.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   77 | main()
      | ^~~~
bottle.cpp: In function 'int main()':
bottle.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
bottle.cpp:81:9: note: in expansion of macro 'fin'
   81 |         fin(task);
      |         ^~~
bottle.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".out","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
bottle.cpp:82:9: note: in expansion of macro 'fou'
   82 |         fou(task);
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...