zoukankan      html  css  js  c++  java
  • Codeforces Round #467 (Div. 1) B. Sleepy Game

    我一开始把题目看错了 我以为是博弈。。
    这题就是一个简单的判环+dfs(不简单,挺烦的一题)

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <queue>
    #include <vector>
    const int N = 2e5 + 5;
    
    struct GraphNode {
        int toVertex, nextPoint;
    } GraphTable[N];
    int headOfVertex[N];
    bool visitedVertex[N][2];
    int visitedCircle[N];
    class Graph {
       private:
        int startVertex_;
        int totEdge_;
        int hasCircle_;
        int hasSuccess_;
        std::vector<int> ansPath;
        bool dfsGraph(int vertex, int step) {
            if (headOfVertex[vertex] == -1) {
                //     printf("%d %d
    ", vertex, headOfVertex[vertex]);
                return step & 1;
            }
            for (int i = headOfVertex[vertex]; ~i; i = GraphTable[i].nextPoint) {
                int toVertex = GraphTable[i].toVertex;
                if (!visitedVertex[toVertex][step]) {
                    visitedVertex[toVertex][step] = true;
                    bool hasSuccess = dfsGraph(toVertex, step ^ 1);
                    if (hasSuccess) {
                        ansPath.push_back(toVertex);
                        //   printf("to %d
    ", toVertex);
                        return true;
                    }
                }
            }
            return false;
        }
        bool judgeCircle(int vertex) {
            visitedCircle[vertex] = -1;
            for (int i = headOfVertex[vertex]; ~i; i = GraphTable[i].nextPoint) {
                int toVertex = GraphTable[i].toVertex;
                if (visitedCircle[toVertex] == 0) {
                    bool flag = judgeCircle(toVertex);
                    if (flag)
                        return true;
                    visitedCircle[toVertex] = 1;
                } else if (visitedCircle[toVertex] == -1)
                    return true;
            }
            return false;
        }
    
       public:
        Graph() {
            ansPath.clear();
            totEdge_ = 0;
            hasSuccess_ = false;
            hasCircle_ = false;
            memset(visitedVertex, 0, sizeof(visitedVertex));
            memset(visitedCircle, 0, sizeof(visitedCircle));
            memset(headOfVertex, -1, sizeof(headOfVertex));
        }
        void setStartVertex(int startVertex) { startVertex_ = startVertex; }
        void addEdge(int fromVertex, int toVertex) {
            //    printf("%d %d
    ", fromVertex, toVertex);
            GraphTable[totEdge_].toVertex = toVertex;
            GraphTable[totEdge_].nextPoint = headOfVertex[fromVertex];
            headOfVertex[fromVertex] = totEdge_++;
        }
        void solve() {
            visitedVertex[startVertex_][1] = true;
            hasCircle_ = judgeCircle(startVertex_);
            hasSuccess_ = dfsGraph(startVertex_, 0);
    
            if (hasSuccess_) {
                ansPath.push_back(startVertex_);
                reverse(ansPath.begin(), ansPath.end());
                printf("Win
    ");
                for (int i = 0; i < ansPath.size(); ++i)
                    printf("%d ", ansPath[i]);
                printf("
    ");
            } else if (hasCircle_)
                printf("Draw
    ");
            else
                printf("Lose
    ");
        }
    };
    
    int main() {
        int n, m;
        while (~scanf("%d %d", &n, &m)) {
            Graph Basa = Graph();
            for (int i = 1; i <= n; ++i) {
                int numberOfEdge;
                scanf("%d", &numberOfEdge);
                for (int j = 0; j < numberOfEdge; ++j) {
                    int toVertex;
                    scanf("%d", &toVertex);
                    Basa.addEdge(i, toVertex);
                }
            }
            int startVertex;
            scanf("%d", &startVertex);
            Basa.setStartVertex(startVertex);
            Basa.solve();
        }
        return 0;
    }
    
  • 相关阅读:
    Flutter 布局类组件:简介
    Flutter 基础组件:进度指示器
    Flutter 基础组件:单选框和复选框
    Flutter 基础组件:图片和Icon
    Flutter 基础组件:按钮
    Flutter 基础组件:文本、字体样式
    Flutter 基础组件:状态管理
    Flutter 基础组件:Widget简介
    网络编程之TCP
    入门多线程
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8529164.html
Copyright © 2011-2022 走看看