9 条评论

  • @ 2026-8-6 21:07:37
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 2e5 + 10;
    int a[N];
    vector<int> vec[N];
    int odd[N], even[N];
    // odd[i]:离i号最近的奇数格子需要几步
    // even[i]:离i号最近的偶数格子需要几步
    bool st[N];
    int main() {
        int n;
        cin >> n;
        queue<int> q;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            int l = i - a[i];
            int r = i + a[i];
            if (l >= 1) {
                vec[l].push_back(i);
            }
            if (r <= n) {
                vec[r].push_back(i);
            }
            if (a[i] % 2 == 1) {
                q.push(i);
                st[i] = true;
            }
        }
        while (q.size()) {
            int t = q.front();
            q.pop();
            for (int x : vec[t]) {
                if (!st[x]) {
                    st[x] = true;
                    odd[x] = odd[t] + 1;
                    q.push(x);
                }
            }
        }
        memset(st, false, sizeof st);
        for (int i = 1; i <= n; i++) {
            if (a[i] % 2 == 0) {
                q.push(i);
                st[i] = true;
            }
        }
        while (q.size()) {
            int t = q.front();
            q.pop();
            for (int x : vec[t]) {
                if (!st[x]) {
                    st[x] = true;
                    even[x] = even[t] + 1;
                    q.push(x);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            if (a[i] % 2 == 0) {
                if (odd[i]) {
                    cout << odd[i] << " ";
                } else
                    cout << -1 << " ";
            }
            if (a[i] % 2 == 1) {
                if (even[i]) {
                    cout << even[i] << " ";
                } else {
                    cout << -1 << ' ';
                }
            }
        }
        return 0;
    }
    
    🤡 1
    • @ 2026-8-6 20:55:32
      #include <bits/stdc++.h>
      using namespace std;
      const int N = 110;
      // vec:邻接表
      // cnt[i]:会i号语言的队员
      vector<int> vec[N], cnt[N];
      bool st[N];
      void dfs(int u) {
          for (int x : vec[u]) {
              if (!st[x]) {
                  st[x] = true;
                  dfs(x);
              }
          }
      }
      int main() {
          int n, m;
          cin >> n >> m;
          int sum = 0;
          for (int i = 1; i <= n; i++) {
              int k;
              cin >> k;
              sum += k;
              while (k--) {
                  int x;
                  cin >> x;
                  cnt[x].push_back(i);
              }
          }
          for (int i = 1; i <= m; i++) {
              if (cnt[i].size() >= 2) {
                  for (int j = 1; j < cnt[i].size(); j++) {
                      int a = cnt[i][j - 1], b = cnt[i][j];
                      vec[a].push_back(b);
                      vec[b].push_back(a);
                  }
              }
          }
          int cnt = 0;
          for (int i = 1; i <= n; i++) {
              if (!st[i]) {
                  st[i] = true;
                  dfs(i);
                  cnt++;
              }
          }
          cout << cnt - (sum != 0) << '\n';
          return 0;
      }
      
      👍 3
      😄 3
      👎 2
      😕 2
      ❤️ 2
      🤔 2
      🤣 2
      🌿 2
      🍋 2
      🕊️ 2
      🤡 2
      👀 2
      • @ 2026-8-6 20:39:49
        #include <bits/stdc++.h>
        using namespace std;
        const int N = 1e6 + 10;
        bool st[N];
        int dist[N];
        int main() {
            long long a, n;
            cin >> a >> n;
            queue<long long> q;
            dist[1] = 0;
            st[1] = true;
            q.push(1);
            while (q.size()) {
                int t = q.front();
                q.pop();
                long long k1 = a * t;
                if (k1 <= 1e6 && !st[k1]) {
                    dist[k1] = dist[t] + 1;
                    st[k1] = true;
                    q.push(k1);
                }
                if (t % 10 != 0 && t >= 10) {
                    string s = to_string(t); //"123"
                    s = s.back() + s;        //'3'+"123" = "3123"
                    s.pop_back();            //"312"
                    long long k1 = stoi(s);  // 312
                    if (k1 <= 1e6 && !st[k1]) {
                        dist[k1] = dist[t] + 1;
                        st[k1] = true;
                        q.push(k1);
                    }
                }
            }
            if (st[n]) {
                cout << dist[n] << '\n';
            } else {
                cout << -1 << '\n';
            }
            return 0;
        }
        
        🤡 7
        🌿 7
        👍 7
        🤔 6
        👎 6
        ❤️ 6
        😄 6
        😕 6
        👀 6
        🕊️ 6
        🍋 6
        🤣 6
        • @ 2026-8-5 21:43:02
          #include <bits/stdc++.h>
          using namespace std;
          typedef pair<int, int> PII;
          const int N = 110;
          char c[N][N];
          int n, m, tim[N][N], dist[N][N];
          int dx[] = {1, -1, 0, 0};
          int dy[] = {0, 0, 1, -1};
          // tim[i][j]:求火源到(i,j)的时间
          // dist[i][j]:起点到达(i,j)的时间
          bool st[N][N];
          void bfs(int sx, int sy) {
              memset(st, false, sizeof st);
              queue<PII> q;
              q.push({sx, sy});
              st[sx][sy] = true;
              tim[sx][sy] = 0;
              while (q.size()) {
                  auto it = q.front();
                  q.pop();
                  int x = it.first, y = it.second;
                  for (int i = 0; i < 4; i++) {
                      int nx = x + dx[i], ny = y + dy[i];
                      if (nx < 1 || nx > n || ny < 1 || ny > m)
                          continue;
                      if (st[nx][ny] || c[nx][ny] == '#')
                          continue;
                      st[nx][ny] = true;
                      tim[nx][ny] = tim[x][y] + 1;
                      q.push({nx, ny});
                  }
              }
          }
          void bfs2(int sx, int sy) {
              memset(st, false, sizeof st);
              queue<PII> q;
              q.push({sx, sy});
              st[sx][sy] = true;
              dist[sx][sy] = 0;
              while (q.size()) {
                  auto it = q.front();
                  q.pop();
                  int x = it.first, y = it.second;
                  for (int i = 0; i < 4; i++) {
                      int nx = x + dx[i], ny = y + dy[i];
                      if (nx < 1 || nx > n || ny < 1 || ny > m)
                          continue;
                      if (st[nx][ny] || c[nx][ny] == '#' || dist[x][y] + 1 >= tim[nx][ny])
                          continue;
                      st[nx][ny] = true;
                      dist[nx][ny] = dist[x][y] + 1;
                      q.push({nx, ny});
                  }
              }
          }
          int main() {
              int t;
              cin >> t;
              while (t--) {
                  cin >> n >> m;
                  int sx, sy, ex, ey; // 起点坐标和终点坐标
                  int fx, fy;         // 火源坐标
                  for (int i = 1; i <= n; i++) {
                      for (int j = 1; j <= m; j++) {
                          cin >> c[i][j];
                          if (c[i][j] == 'S') {
                              sx = i, sy = j;
                          }
                          if (c[i][j] == 'E') {
                              ex = i, ey = j;
                          }
                          if (c[i][j] == 'F') {
                              fx = i, fy = j;
                          }
                      }
                  }
                  bfs(fx, fy);
                  bfs2(sx, sy);
                  if (st[ex][ey]) {
                      cout << "YES" << '\n';
                  } else {
                      cout << "NO" << '\n';
                  }
              }
              return 0;
          }
          
          🤡 4
          • @ 2026-8-5 21:38:46
            #include <bits/stdc++.h>
            using namespace std;
            typedef pair<int, int> PII;
            const int N = 510;
            int a[N][N], dist[N][N];
            int n, m, k;
            bool st[N][N];
            int dx[] = {1, -1, 0, 0};
            int dy[] = {0, 0, 1, -1};
            bool check(int mid) {
                memset(st, false, sizeof st);
                memset(dist, 0, sizeof dist);
                queue<PII> q;
                q.push({1, 1});
                dist[1][1] = 0;
                st[1][1] = false;
                while (q.size()) {
                    auto it = q.front();
                    q.pop();
                    int x = it.first, y = it.second;
                    for (int i = 0; i < 4; i++) {
                        int nx = x + dx[i], ny = y + dy[i];
                        if (nx < 1 || nx > n || ny < 1 || ny > m)
                            continue;
                        // 判断是否走过 以及 mid等级是否够用
                        if (st[nx][ny] || a[nx][ny] > mid)
                            continue;
                        dist[nx][ny] = dist[x][y] + 1;
                        st[nx][ny] = true;
                        q.push({nx, ny});
                    }
                }
                if (st[n][m] && dist[n][m] <= k) {
                    return true;
                }
                return false;
            }
            int main() {
                cin >> n >> m >> k;
                for (int i = 1; i <= n; i++) {
                    for (int j = 1; j <= m; j++) {
                        cin >> a[i][j];
                    }
                }
                int l = 0, r = 1e9, ans = -1;
                while (l <= r) {
                    int mid = (l + r) / 2;
                    if (check(mid)) {
                        ans = mid;
                        r = mid - 1;
                    } else {
                        l = mid + 1;
                    }
                }
                cout << ans << '\n';
                return 0;
            }
            
            • @ 2026-8-5 19:17:18
              #include <bits/stdc++.h>
              using namespace std ;
              
              struct Pos {
              	int _x ;
              	int _y ;
              };
              
              const int LEN = 1e3 + 10 ;
              int place [LEN][LEN] = {};
              bool is_lev [LEN][LEN]= {};
              int ans [LEN][LEN] = {};
              
              int n , m , k ;
              queue <Pos> squ_pos ;
              
              int dx[] = {1,-1,0,0};
              int dy[] = {0,0,1,-1};
              
              void Bfs (int need_x , int need_y) {
              	squ_pos.push({1,1}) ;
              	is_lev[1][1] = true ;
              //	ans[1][1] = 1 ;
              	
              	while (squ_pos.size()) {
              		Pos now_squ = squ_pos.front() ;
              		squ_pos.pop() ;
              		
              		for (int i = 0 ; i < 4 ; i++) {
              			int to_x = now_squ._x + dx[i] ;
              			int to_y = now_squ._y + dy[i] ;
              			
              			
              			if ( (to_x < 1) || (to_x > n) || (to_y < 1) || (to_y > m) )
              				continue ;
              			
              			if (place[to_x][to_y]-place[now_squ._x][now_squ._y] > k || is_lev[to_x][to_y])
              				continue ;
              			
              			ans[to_x][to_y] = ans[now_squ._x][now_squ._y] + 1 ;
              			
              			if (to_x == n && to_y == m){
              				cout << ans[to_x][to_y] ;
              				return ;
              			}
              			
              			squ_pos.push({to_x , to_y}) ;
              			is_lev[to_x][to_y] = true ;
              		}
              	}
              	
              	cout << "-1" ;
              	return ;
              }
              
              int main() {
              	
              	cin >> n >> m >> k ;
              	
              	for (int i = 1 ; i <= n ; i++){
              		for (int j = 1 ; j <= m ; j++){
              			cin >> place[i][j] ;
              		}
              	}
              	
              	Bfs(1,1) ;
              	
              	return 0 ;
              }
              
              🤔 4
              👀 4
              👎 3
              • @ 2026-8-2 21:06:00

                • @ 2026-8-2 21:05:15

                  🌿 2
                  • @ 2026-8-2 21:04:33
                    #include <bits/stdc++.h>
                    using namespace std;
                    int ans = 0;
                    set<pair<int, int>> s;
                    void black(int l, int r) {
                        auto it = s.lower_bound({l, -1e9});
                        if (it != s.begin()) {
                            auto pre = it;
                            --pre;
                            if (pre->second >= l - 1) {
                                it = pre;
                            }
                        }
                        while (it != s.end() && it->first <= r + 1) {
                            l = min(l, it->first);
                            r = max(r, it->second);
                            ans -= it->second - it->first + 1;
                            it = s.erase(it);
                        }
                        s.insert({l, r});
                        ans += (r - l + 1);
                    }
                    void white(int l, int r) {
                        auto it = s.lower_bound({l, -1e9});
                        if (it != s.begin()) {
                            auto pre = it;
                            pre--;
                            if (pre->second >= l) {
                                it = pre;
                            }
                        }
                        while (it != s.end() && it->first <= r) {
                            int a = it->first;
                            int b = it->second;
                            ans -= b - a + 1;
                            it = s.erase(it);
                            if (a < l) {
                                s.insert({a, l - 1});
                                ans += l - a;
                            }
                            if (b > r) {
                                s.insert({r + 1, b});
                                ans += b - r;
                                break;
                            }
                        }
                    }
                    int main() {
                        int q;
                        cin >> q;
                        while (q--) {
                            int op, l, r;
                            cin >> op;
                            if (op == 1) {
                                cin >> l >> r;
                                black(l, r);
                            } else if (op == 2) {
                                cin >> l >> r;
                                white(l, r);
                            } else {
                                cout << ans << '\n';
                            }
                        }
                        return 0;
                    }
                    
                    👍 5
                    🤔 4
                    👎 3
                    🤡 3
                    • 1