QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#86568#4231. Rafting Tripcardinal_city#WA 4ms19548kbC++206.2kb2023-03-10 09:41:072023-03-10 09:41:11

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-10 09:41:11]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:19548kb
  • [2023-03-10 09:41:07]
  • 提交

answer

#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
 
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
 
template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
 
const ll MOD  = 998244353;
#define inf 1e18;
#define INF INT_MAX

long double PI = 4*atan(1);
long double eps = 1e-12;




int bins[300000] = {};
int r,c; 
int dr[4] = {0,0,1,-1};
int dc[4] = {1,-1,0,0};
map<char, int> dir = {{'>', 0}, {'<', 1}, {'v', 2}, {'^', 3}, {'.', -1}, {'#', -1}};

int inv[300000] = {};
int endchain[300000] = {};
vector<int> cycidx(300000, -1);
vector<int> nxt(300000, -1000000);
vector<int> adjbins[300000];
vector<int> padj[300000];


vector<unordered_set<int>> cycbins;
vector<vector<int>> cycs;
int numcyc = 0;
int vis[300000] = {};

int counter[300000] = {};
int ans = 0;
int tot = 0;


int to_idx(int i, int j) {
    return (c+2) * i + j;
}

void dfs(int u) {
    if(vis[u] == 2) return;
    if(vis[u] == 1) {
        int v = nxt[u];
        vector<int> cyc = {u};
        while(v != u) {
            cyc.push_back(v);
            v = nxt[v];
        }
        unordered_set<int> cbins;
        for(auto x : cyc) {
            for(auto b : adjbins[x]) {
                cbins.insert(b);
            }
            cycidx[x] = numcyc;
        }
        cycbins.push_back(cbins);
        cycs.push_back(cyc);
        numcyc++;
        return;
    }
    vis[u] = 1;
    if(nxt[u] != -1000000) dfs(nxt[u]);
    vis[u] = 2;
    return;
}

void updatecounter(int u, int add) {
  if(add) {
    for(int b : adjbins[u]) {
      if(counter[b] == 0) {
        tot++;
        ans = max(ans, tot);
      }
      counter[b]++;
    }
  } else {
    for(int b : adjbins[u]) {
      counter[b]--;
      if(counter[b] == 0) {
        tot--;
      }
    }
  }
  
}


void dfs2(int u) {

  updatecounter(u, true);
//  cout << u << " " << tot << endl;
  for(int v : padj[u]) {
    dfs2(v);
  }
  updatecounter(u, false);

}

int main() {
    //auto start = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);cin.tie(0);
    //ofstream cout("output.txt");
    //ifstream cin("input.txt");
    #ifdef DEBUG
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  

     cin >> r >> c;
    char g[510][510] = {};
    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++) {
            cin >> g[i][j];
            if(g[i][j] == '#') bins[to_idx(i,j)] = 1;
        }
    }

    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++) {
            int d = dir[g[i][j]];
            int idx = to_idx(i,j);
            if(d >= 0) {
                int ni = i + dr[d];
                int nj = j + dc[d];
                int nidx = to_idx(ni, nj);
                inv[nidx]++;
                nxt[idx] = nidx;
                padj[nidx].pb(idx);
                if(ni == 0 || ni == r+1 || nj == 0 || nj == c+1 || dir[g[ni][nj]] == -1) endchain[idx] = 1;
                for(int k = 0; k < 4; k++) {
                    int ki = i + dr[k];
                    int kj = j + dc[k];
                    int kidx = to_idx(ki, kj);
                    if(bins[kidx]) adjbins[idx].push_back(kidx);
                }
            }
        }
    }

    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++) {
            int idx = to_idx(i,j);
            if(vis[idx] == 0 && nxt[idx] != -1000000) {
                dfs(idx);
            }
        }
    }

    for(int i = 0; i < cycs.size(); i++) {
      
      vector<int> cyc = cycs[i];
      pr(cyc);
      for(int b : cycbins[i]) {
        if(counter[b] == 0) {
          tot++;
          ans = max(ans, tot);
        }
        counter[b]++;
      }
     

      for(int u : cyc) {
        for(int v : padj[u]) {
          if(cycidx[v] == -1) dfs2(v);
        }
      }

      for(int b : cycbins[i]) {
        counter[b]--;
        if(counter[b] == 0) {
          tot--;
        }
      }
    }

    for(int i = 1; i <= r; i++) {
        for(int j = 1; j <= c; j++) {
            int idx = to_idx(i,j);
            if(endchain[idx]) {
              dfs2(idx);
            }
        }
    }


    

    cout << ans << endl;

    
    return 0;


    //auto stop = chrono::high_resolution_clock::now();
    //auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
    //cout << duration.count() << endl;
    //cin.close();
    //cout.close();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 19536kb

input:

5 6
v<<<#v
v#v<.>
>>v<<<
..v##^
#<<<.^

output:

4

result:

ok single line: '4'

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 19548kb

input:

4 5
>v<<.
^<..#
#...#
.#>^#

output:

8 9 16 15 
2

result:

wrong answer 1st lines differ - expected: '2', found: '8 9 16 15 '