library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/aoj/3047.test.cpp

Depends on

Code

// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3047

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../maxflow/dinic.cpp"
#undef call_from_test

const int K = 28;
int cap[K][K];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;
  vector<string> s(n);
  for(int i=0;i<n;i++) cin>>s[i];

  for(int i=0;i<26;i++){
    memset(cap,0,sizeof(cap));

    int x=26,y=27,z=0,e=0;
    Dinic<int, true> G(K);

    for(int j=0;j<n;j++){
      int u=s[j].front()-'a';
      int v=s[j].back()-'a';
      if(u==i) z++;
      if(v==i) e++;
      if(u==i) u=x;
      if(v==i) v=y;
      if(u!=v) cap[u][v]++;
    }

    for(int u=0;u<K;u++)
      for(int v=0;v<K;v++)
        if(cap[u][v]) G.add_edge(u,v,cap[u][v]);

    if((e&&!z)||(z&&G.flow(x,y)==z)) cout<<char('a'+i)<<endl;
  }

  return 0;
}
#line 1 "test/aoj/3047.test.cpp"
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3047

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "maxflow/dinic.cpp"

#line 3 "maxflow/dinic.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// O(E V^2)
// O(E \min(E^{1/2}, V^{2/3})) if caps are constant
template<typename Flow,bool directed>
struct Dinic{
  struct Edge {
    int dst;
    Flow cap;
    int rev;
    Edge(int dst,Flow cap,int rev):dst(dst),cap(cap),rev(rev){}
  };

  vector< vector<Edge> > G;
  vector<int> level,iter;

  Dinic(int n):G(n),level(n),iter(n){}

  int add_edge(int src,int dst,Flow cap){
    int e=G[src].size();
    int r=(src==dst?e+1:G[dst].size());
    G[src].emplace_back(dst,cap,r);
    G[dst].emplace_back(src,directed?0:cap,e);
    return e;
  }

  void bfs(int s){
    fill(level.begin(),level.end(),-1);
    queue<int> que;
    level[s]=0;
    que.emplace(s);
    while(!que.empty()){
      int v=que.front();que.pop();
      for(Edge &e : G[v]) {
        if(e.cap>0 and level[e.dst]<0){
          level[e.dst]=level[v]+1;
          que.emplace(e.dst);
        }
      }
    }
  }

  Flow dfs(int v,int t,Flow f){
    if(v==t) return f;
    for(int &i=iter[v];i<(int)G[v].size();i++){
      Edge &e=G[v][i];
      if(e.cap>0 and level[v]<level[e.dst]){
        Flow d=dfs(e.dst,t,min(f,e.cap));
        if(d==0) continue;
        e.cap-=d;
        G[e.dst][e.rev].cap+=d;
        return d;
      }
    }
    return 0;
  }

  Flow flow(int s,int t,Flow lim){
    Flow fl=0;
    while(1){
      bfs(s);
      if(level[t]<0 or lim==0) break;
      fill(iter.begin(),iter.end(),0);

      while(1){
        Flow f=dfs(s,t,lim);
        if(f==0) break;
        fl+=f;
        lim-=f;
      }
    }
    return fl;
  }

  Flow flow(int s,int t){
    return flow(s,t,numeric_limits<Flow>::max()/2);
  }

  Flow cut(int s,int t,int x,int a){
    static_assert(directed, "must be directed");
    auto &e=G[x][a];
    int y=e.dst;
    Flow cr=G[y][e.rev].cap;
    if(cr==0) return e.cap=0;
    e.cap=G[y][e.rev].cap=0;
    Flow cap=cr-flow(x,y,cr);
    if(x!=s and cap!=0) flow(x,s,cap);
    if(t!=y and cap!=0) flow(t,y,cap);
    return cap;
  }

  Flow link(int s,int t,int x,int a,Flow f){
    auto &e=G[x][a];
    e.cap+=f;
    return flow(s,t,f);
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 8 "test/aoj/3047.test.cpp"
#undef call_from_test

const int K = 28;
int cap[K][K];

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;
  vector<string> s(n);
  for(int i=0;i<n;i++) cin>>s[i];

  for(int i=0;i<26;i++){
    memset(cap,0,sizeof(cap));

    int x=26,y=27,z=0,e=0;
    Dinic<int, true> G(K);

    for(int j=0;j<n;j++){
      int u=s[j].front()-'a';
      int v=s[j].back()-'a';
      if(u==i) z++;
      if(v==i) e++;
      if(u==i) u=x;
      if(v==i) v=y;
      if(u!=v) cap[u][v]++;
    }

    for(int u=0;u<K;u++)
      for(int v=0;v<K;v++)
        if(cap[u][v]) G.add_edge(u,v,cap[u][v]);

    if((e&&!z)||(z&&G.flow(x,y)==z)) cout<<char('a'+i)<<endl;
  }

  return 0;
}
Back to top page